From 114165750982c3e3cde6a405f2c499071c4700e7 Mon Sep 17 00:00:00 2001 From: Peter Turi Date: Fri, 7 Feb 2025 19:46:54 +0100 Subject: [PATCH] chore: use lo for error casting (#2229) --- openmeter/notification/webhook/svix.go | 3 +-- pkg/errorsx/convert.go | 11 ----------- pkg/errorsx/handler.go | 6 ++++-- pkg/framework/commonhttp/errors.go | 5 +++-- pkg/framework/transaction/transaction.go | 4 ++-- test/notification/event.go | 4 ++-- test/notification/rule.go | 4 ++-- 7 files changed, 14 insertions(+), 23 deletions(-) delete mode 100644 pkg/errorsx/convert.go diff --git a/openmeter/notification/webhook/svix.go b/openmeter/notification/webhook/svix.go index ac810d243..b3e3e8aaf 100644 --- a/openmeter/notification/webhook/svix.go +++ b/openmeter/notification/webhook/svix.go @@ -18,7 +18,6 @@ import ( "github.com/openmeterio/openmeter/pkg/convert" "github.com/openmeterio/openmeter/pkg/defaultx" - "github.com/openmeterio/openmeter/pkg/errorsx" ) const ( @@ -642,7 +641,7 @@ func unwrapSvixError(err error) error { return nil } - svixErr, ok := errorsx.ErrorAs[*svix.Error](err) + svixErr, ok := lo.ErrorsAs[*svix.Error](err) if !ok { return err } diff --git a/pkg/errorsx/convert.go b/pkg/errorsx/convert.go deleted file mode 100644 index 0c2481029..000000000 --- a/pkg/errorsx/convert.go +++ /dev/null @@ -1,11 +0,0 @@ -package errorsx - -import "errors" - -func ErrorAs[T error](err error) (T, bool) { - var outerr T - if errors.As(err, &outerr) { - return outerr, true - } - return outerr, false -} diff --git a/pkg/errorsx/handler.go b/pkg/errorsx/handler.go index e27ea8ce1..c6be39289 100644 --- a/pkg/errorsx/handler.go +++ b/pkg/errorsx/handler.go @@ -5,6 +5,8 @@ import ( "errors" "log/slog" + "github.com/samber/lo" + "github.com/openmeterio/openmeter/pkg/framework/transport/httptransport" ) @@ -38,7 +40,7 @@ func (h SlogHandler) Handle(err error) { } // Warn errors are logged as warnings. - if wErr, ok := ErrorAs[*warnError](err); ok { + if wErr, ok := lo.ErrorsAs[*warnError](err); ok { h.Logger.Warn(wErr.Error()) return } @@ -55,7 +57,7 @@ func (h SlogHandler) HandleContext(ctx context.Context, err error) { } // Warn errors are logged as warnings. - if wErr, ok := ErrorAs[*warnError](err); ok { + if wErr, ok := lo.ErrorsAs[*warnError](err); ok { h.Logger.WarnContext(ctx, wErr.Error()) return } diff --git a/pkg/framework/commonhttp/errors.go b/pkg/framework/commonhttp/errors.go index 9c65a1f60..5793c5709 100644 --- a/pkg/framework/commonhttp/errors.go +++ b/pkg/framework/commonhttp/errors.go @@ -5,7 +5,8 @@ import ( "errors" "net/http" - "github.com/openmeterio/openmeter/pkg/errorsx" + "github.com/samber/lo" + "github.com/openmeterio/openmeter/pkg/models" ) @@ -59,7 +60,7 @@ func ErrorEncoder(ctx context.Context, _ error, w http.ResponseWriter) bool { // Using the generic feature we can mandate that the error implements the error interface. This is a // must, as the errors.As would panic if the error does not implement the error interface. func HandleErrorIfTypeMatches[T error](ctx context.Context, statusCode int, err error, w http.ResponseWriter, extendedProblemFunc ...func(T) map[string]interface{}) bool { - if err, ok := errorsx.ErrorAs[T](err); ok { + if err, ok := lo.ErrorsAs[T](err); ok { extendedProblemFuncs := make([]ExtendProblemFunc, 0, len(extendedProblemFunc)) for _, f := range extendedProblemFunc { extendedProblemFuncs = append(extendedProblemFuncs, func() map[string]interface{} { diff --git a/pkg/framework/transaction/transaction.go b/pkg/framework/transaction/transaction.go index b6c72f87d..c249fa9b2 100644 --- a/pkg/framework/transaction/transaction.go +++ b/pkg/framework/transaction/transaction.go @@ -6,7 +6,7 @@ import ( "log/slog" "runtime/debug" - "github.com/openmeterio/openmeter/pkg/errorsx" + "github.com/samber/lo" ) // Driver is an interface for transaction drivers @@ -35,7 +35,7 @@ func AddPostCommitHook(ctx context.Context, callback func(ctx context.Context) e hookMgr, err := getHookManagerFromContext(ctx) if err != nil { // If we are not in transaction let's invoke the callback directly - if _, ok := errorsx.ErrorAs[*hookManagerNotFoundError](err); ok { + if _, ok := lo.ErrorsAs[*hookManagerNotFoundError](err); ok { hook(ctx) return } diff --git a/test/notification/event.go b/test/notification/event.go index 6b1a820e0..bbbd7b48f 100644 --- a/test/notification/event.go +++ b/test/notification/event.go @@ -6,6 +6,7 @@ import ( "testing" "time" + "github.com/samber/lo" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -13,7 +14,6 @@ import ( "github.com/openmeterio/openmeter/openmeter/notification" "github.com/openmeterio/openmeter/openmeter/productcatalog/feature" "github.com/openmeterio/openmeter/pkg/convert" - "github.com/openmeterio/openmeter/pkg/errorsx" "github.com/openmeterio/openmeter/pkg/models" ) @@ -114,7 +114,7 @@ func (s *EventTestSuite) Setup(ctx context.Context, t *testing.T) { require.NoError(t, err, "Getting meter must not return error") feat, err := s.Env.Feature().GetFeature(ctx, TestNamespace, TestFeatureKey, false) - if _, ok := errorsx.ErrorAs[*feature.FeatureNotFoundError](err); !ok { + if _, ok := lo.ErrorsAs[*feature.FeatureNotFoundError](err); !ok { require.NoError(t, err, "Getting feature must not return error") } if feat != nil { diff --git a/test/notification/rule.go b/test/notification/rule.go index 6e28e5728..756a272ea 100644 --- a/test/notification/rule.go +++ b/test/notification/rule.go @@ -4,13 +4,13 @@ import ( "context" "testing" + "github.com/samber/lo" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/openmeterio/openmeter/openmeter/notification" "github.com/openmeterio/openmeter/openmeter/productcatalog/feature" "github.com/openmeterio/openmeter/pkg/convert" - "github.com/openmeterio/openmeter/pkg/errorsx" "github.com/openmeterio/openmeter/pkg/models" ) @@ -60,7 +60,7 @@ func (s *RuleTestSuite) Setup(ctx context.Context, t *testing.T) { require.NoError(t, err, "Getting meter must not return error") feat, err := s.Env.Feature().GetFeature(ctx, TestNamespace, TestFeatureKey, false) - if _, ok := errorsx.ErrorAs[*feature.FeatureNotFoundError](err); !ok { + if _, ok := lo.ErrorsAs[*feature.FeatureNotFoundError](err); !ok { require.NoError(t, err, "Getting feature must not return error") } if feat != nil {