Skip to content

Commit

Permalink
refactor: unify error handling for entitlements (#1076)
Browse files Browse the repository at this point in the history
  • Loading branch information
GAlexIHU authored Jun 27, 2024
1 parent 5544239 commit edc19b8
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 118 deletions.
54 changes: 4 additions & 50 deletions internal/entitlement/httpdriver/entitlement.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
meteredentitlement "github.com/openmeterio/openmeter/internal/entitlement/metered"
staticentitlement "github.com/openmeterio/openmeter/internal/entitlement/static"
"github.com/openmeterio/openmeter/internal/namespace/namespacedriver"
"github.com/openmeterio/openmeter/internal/productcatalog"
"github.com/openmeterio/openmeter/pkg/convert"
"github.com/openmeterio/openmeter/pkg/defaultx"
"github.com/openmeterio/openmeter/pkg/framework/commonhttp"
Expand Down Expand Up @@ -138,38 +137,7 @@ func (h *entitlementHandler) CreateEntitlement() CreateEntitlementHandler {
httptransport.AppendOptions(
h.options,
httptransport.WithOperationName("createEntitlement"),
httptransport.WithErrorEncoder(func(ctx context.Context, err error, w http.ResponseWriter) bool {
if _, ok := err.(*productcatalog.FeatureNotFoundError); ok {
commonhttp.NewHTTPError(
http.StatusNotFound,
err,
).EncodeError(ctx, w)
return true
}
if _, ok := err.(*entitlement.NotFoundError); ok {
commonhttp.NewHTTPError(
http.StatusNotFound,
err,
).EncodeError(ctx, w)
return true
}
if err, ok := err.(*entitlement.AlreadyExistsError); ok {
commonhttp.NewHTTPError(
http.StatusConflict,
err,
commonhttp.ExtendProblem("conflictingEntityId", err.EntitlementID),
).EncodeError(ctx, w)
return true
}
if err, ok := err.(*entitlement.InvalidValueError); ok {
commonhttp.NewHTTPError(
http.StatusBadRequest,
err,
).EncodeError(ctx, w)
return true
}
return false
}),
httptransport.WithErrorEncoder(getErrorEncoder()),
)...,
)
}
Expand Down Expand Up @@ -235,23 +203,7 @@ func (h *entitlementHandler) GetEntitlementValue() GetEntitlementValueHandler {
httptransport.AppendOptions(
h.options,
httptransport.WithOperationName("getEntitlementValue"),
httptransport.WithErrorEncoder(func(ctx context.Context, err error, w http.ResponseWriter) bool {
if _, ok := err.(*productcatalog.FeatureNotFoundError); ok {
commonhttp.NewHTTPError(
http.StatusNotFound,
err,
).EncodeError(ctx, w)
return true
}
if _, ok := err.(*entitlement.NotFoundError); ok {
commonhttp.NewHTTPError(
http.StatusNotFound,
err,
).EncodeError(ctx, w)
return true
}
return false
}),
httptransport.WithErrorEncoder(getErrorEncoder()),
)...,
)
}
Expand Down Expand Up @@ -299,6 +251,7 @@ func (h *entitlementHandler) GetEntitlementsOfSubjectHandler() GetEntitlementsOf
httptransport.AppendOptions(
h.options,
httptransport.WithOperationName("getEntitlementsOfSubject"),
httptransport.WithErrorEncoder(getErrorEncoder()),
)...,
)
}
Expand Down Expand Up @@ -355,6 +308,7 @@ func (h *entitlementHandler) ListEntitlements() ListEntitlementsHandler {
httptransport.AppendOptions(
h.options,
httptransport.WithOperationName("listEntitlements"),
httptransport.WithErrorEncoder(getErrorEncoder()),
)...,
)
}
Expand Down
70 changes: 70 additions & 0 deletions internal/entitlement/httpdriver/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package httpdriver

import (
"context"
"net/http"

"github.com/openmeterio/openmeter/internal/entitlement"
"github.com/openmeterio/openmeter/internal/productcatalog"
"github.com/openmeterio/openmeter/pkg/framework/commonhttp"
"github.com/openmeterio/openmeter/pkg/framework/transport/httptransport"
"github.com/openmeterio/openmeter/pkg/models"
)

func getErrorEncoder() httptransport.ErrorEncoder {
return func(ctx context.Context, err error, w http.ResponseWriter) bool {
// user errors
if _, ok := err.(*productcatalog.FeatureNotFoundError); ok {
commonhttp.NewHTTPError(
http.StatusNotFound,
err,
).EncodeError(ctx, w)
return true
}
if _, ok := err.(*entitlement.NotFoundError); ok {
commonhttp.NewHTTPError(
http.StatusNotFound,
err,
).EncodeError(ctx, w)
return true
}
if _, ok := err.(*models.GenericUserError); ok {
commonhttp.NewHTTPError(
http.StatusBadRequest,
err,
).EncodeError(ctx, w)
return true
}
if err, ok := err.(*entitlement.AlreadyExistsError); ok {
commonhttp.NewHTTPError(
http.StatusConflict,
err,
commonhttp.ExtendProblem("conflictingEntityId", err.EntitlementID),
).EncodeError(ctx, w)
return true
}
if err, ok := err.(*entitlement.InvalidValueError); ok {
commonhttp.NewHTTPError(
http.StatusBadRequest,
err,
).EncodeError(ctx, w)
return true
}
if err, ok := err.(*entitlement.InvalidFeatureError); ok {
commonhttp.NewHTTPError(
http.StatusBadRequest,
err,
).EncodeError(ctx, w)
return true
}
// system errors (naming known errors for transparency)
if _, ok := err.(*entitlement.WrongTypeError); ok {
commonhttp.NewHTTPError(
http.StatusInternalServerError,
err,
).EncodeError(ctx, w)
return true
}
return false
}
}
72 changes: 4 additions & 68 deletions internal/entitlement/httpdriver/metered.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,7 @@ func (h *meteredEntitlementHandler) CreateGrant() CreateGrantHandler {
commonhttp.JSONResponseEncoderWithStatus[api.EntitlementGrant](http.StatusCreated),
httptransport.AppendOptions(
h.options,
httptransport.WithErrorEncoder(func(ctx context.Context, err error, w http.ResponseWriter) bool {
if _, ok := err.(*entitlement.NotFoundError); ok {
commonhttp.NewHTTPError(
http.StatusNotFound,
err,
).EncodeError(ctx, w)
return true
}
if _, ok := err.(*models.GenericUserError); ok {
commonhttp.NewHTTPError(
http.StatusBadRequest,
err,
).EncodeError(ctx, w)
return true
}
return false
}),
httptransport.WithErrorEncoder(getErrorEncoder()),
)...,
)
}
Expand Down Expand Up @@ -194,23 +178,7 @@ func (h *meteredEntitlementHandler) ListEntitlementGrants() ListEntitlementGrant
commonhttp.JSONResponseEncoder[[]api.EntitlementGrant],
httptransport.AppendOptions(
h.options,
httptransport.WithErrorEncoder(func(ctx context.Context, err error, w http.ResponseWriter) bool {
if _, ok := err.(*entitlement.NotFoundError); ok {
commonhttp.NewHTTPError(
http.StatusNotFound,
err,
).EncodeError(ctx, w)
return true
}
if _, ok := err.(*models.GenericUserError); ok {
commonhttp.NewHTTPError(
http.StatusBadRequest,
err,
).EncodeError(ctx, w)
return true
}
return false
}),
httptransport.WithErrorEncoder(getErrorEncoder()),
)...,
)
}
Expand Down Expand Up @@ -264,23 +232,7 @@ func (h *meteredEntitlementHandler) ResetEntitlementUsage() ResetEntitlementUsag
commonhttp.EmptyResponseEncoder[interface{}](http.StatusNoContent),
httptransport.AppendOptions(
h.options,
httptransport.WithErrorEncoder(func(ctx context.Context, err error, w http.ResponseWriter) bool {
if _, ok := err.(*entitlement.NotFoundError); ok {
commonhttp.NewHTTPError(
http.StatusNotFound,
err,
).EncodeError(ctx, w)
return true
}
if _, ok := err.(*models.GenericUserError); ok {
commonhttp.NewHTTPError(
http.StatusBadRequest,
err,
).EncodeError(ctx, w)
return true
}
return false
}),
httptransport.WithErrorEncoder(getErrorEncoder()),
)...,
)
}
Expand Down Expand Up @@ -378,23 +330,7 @@ func (h *meteredEntitlementHandler) GetEntitlementBalanceHistory() GetEntitlemen
commonhttp.JSONResponseEncoder[api.WindowedBalanceHistory],
httptransport.AppendOptions(
h.options,
httptransport.WithErrorEncoder(func(ctx context.Context, err error, w http.ResponseWriter) bool {
if _, ok := err.(*entitlement.NotFoundError); ok {
commonhttp.NewHTTPError(
http.StatusNotFound,
err,
).EncodeError(ctx, w)
return true
}
if _, ok := err.(*models.GenericUserError); ok {
commonhttp.NewHTTPError(
http.StatusBadRequest,
err,
).EncodeError(ctx, w)
return true
}
return false
}),
httptransport.WithErrorEncoder(getErrorEncoder()),
)...,
)
}
Expand Down

0 comments on commit edc19b8

Please sign in to comment.