Skip to content

Commit

Permalink
chore: use lo for error casting (#2229)
Browse files Browse the repository at this point in the history
  • Loading branch information
turip authored Feb 7, 2025
1 parent 2c1eb7d commit 1141657
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 23 deletions.
3 changes: 1 addition & 2 deletions openmeter/notification/webhook/svix.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (

"github.com/openmeterio/openmeter/pkg/convert"
"github.com/openmeterio/openmeter/pkg/defaultx"
"github.com/openmeterio/openmeter/pkg/errorsx"
)

const (
Expand Down Expand Up @@ -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
}
Expand Down
11 changes: 0 additions & 11 deletions pkg/errorsx/convert.go

This file was deleted.

6 changes: 4 additions & 2 deletions pkg/errorsx/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"log/slog"

"github.com/samber/lo"

"github.com/openmeterio/openmeter/pkg/framework/transport/httptransport"
)

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/framework/commonhttp/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"errors"
"net/http"

"github.com/openmeterio/openmeter/pkg/errorsx"
"github.com/samber/lo"

"github.com/openmeterio/openmeter/pkg/models"
)

Expand Down Expand Up @@ -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{} {
Expand Down
4 changes: 2 additions & 2 deletions pkg/framework/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions test/notification/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"testing"
"time"

"github.com/samber/lo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/openmeterio/openmeter/api"
"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"
)

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions test/notification/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 1141657

Please sign in to comment.