Skip to content

Commit

Permalink
Merge pull request #1142 from openmeterio/feature/bring-back-http-req…
Browse files Browse the repository at this point in the history
…uest-to-error-encoders

feat: add support request in error encorder
  • Loading branch information
turip authored Jul 4, 2024
2 parents 92478a7 + 6347ab2 commit e394f7e
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions internal/credit/httpdriver/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (h *grantHandler) ListGrants() ListGrantsHandler {
commonhttp.JSONResponseEncoder[[]api.EntitlementGrant],
httptransport.AppendOptions(
h.options,
httptransport.WithErrorEncoder(func(ctx context.Context, err error, w http.ResponseWriter) bool {
httptransport.WithErrorEncoder(func(ctx context.Context, err error, w http.ResponseWriter, _ *http.Request) bool {
if _, ok := err.(*models.GenericUserError); ok {
commonhttp.NewHTTPError(
http.StatusBadRequest,
Expand Down Expand Up @@ -138,7 +138,7 @@ func (h *grantHandler) VoidGrant() VoidGrantHandler {
commonhttp.EmptyResponseEncoder[interface{}](http.StatusNoContent),
httptransport.AppendOptions(
h.options,
httptransport.WithErrorEncoder(func(ctx context.Context, err error, w http.ResponseWriter) bool {
httptransport.WithErrorEncoder(func(ctx context.Context, err error, w http.ResponseWriter, _ *http.Request) bool {
if _, ok := err.(*models.GenericUserError); ok {
commonhttp.NewHTTPError(
http.StatusBadRequest,
Expand Down
2 changes: 1 addition & 1 deletion internal/entitlement/httpdriver/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func getErrorEncoder() httptransport.ErrorEncoder {
return func(ctx context.Context, err error, w http.ResponseWriter) bool {
return func(ctx context.Context, err error, w http.ResponseWriter, r *http.Request) bool {
// user errors
return commonhttp.HandleErrorIfTypeMatches[*productcatalog.FeatureNotFoundError](ctx, http.StatusNotFound, err, w) ||
commonhttp.HandleErrorIfTypeMatches[*entitlement.NotFoundError](ctx, http.StatusNotFound, err, w) ||
Expand Down
4 changes: 2 additions & 2 deletions internal/ingest/ingestdriver/http_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ type ingestEventsErrorEncoder struct {
CommonErrorEncoder httptransport.ErrorEncoder
}

func (e ingestEventsErrorEncoder) encode(ctx context.Context, err error, w http.ResponseWriter) bool {
func (e ingestEventsErrorEncoder) encode(ctx context.Context, err error, w http.ResponseWriter, r *http.Request) bool {
if e := (ErrorInvalidContentType{}); errors.As(err, &e) {
models.NewStatusProblem(ctx, e, http.StatusBadRequest).Respond(w)

Expand All @@ -141,7 +141,7 @@ func (e ingestEventsErrorEncoder) encode(ctx context.Context, err error, w http.
}

if e.CommonErrorEncoder != nil {
return e.CommonErrorEncoder(ctx, err, w)
return e.CommonErrorEncoder(ctx, err, w, r)
}

models.NewStatusProblem(ctx, errors.New("something went wrong"), http.StatusInternalServerError).Respond(w)
Expand Down
2 changes: 1 addition & 1 deletion internal/productcatalog/httpdriver/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func getErrorEncoder() httptransport.ErrorEncoder {
return func(ctx context.Context, err error, w http.ResponseWriter) bool {
return func(ctx context.Context, err error, w http.ResponseWriter, r *http.Request) bool {
return commonhttp.HandleErrorIfTypeMatches[*productcatalog.FeatureNotFoundError](ctx, http.StatusNotFound, err, w) ||
commonhttp.HandleErrorIfTypeMatches[*productcatalog.FeatureInvalidFiltersError](ctx, http.StatusBadRequest, err, w) ||
commonhttp.HandleErrorIfTypeMatches[*productcatalog.FeatureInvalidMeterAggregationError](ctx, http.StatusBadRequest, err, w) ||
Expand Down
10 changes: 5 additions & 5 deletions pkg/framework/transport/httptransport/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type ResponseEncoder[Response any] func(ctx context.Context, w http.ResponseWrit
// Users are encouraged to use custom ErrorEncoders to encode HTTP errors to
// their clients, and will likely want to pass and check for their own error
// types. See the example shipping/handling service.
type ErrorEncoder func(ctx context.Context, err error, w http.ResponseWriter) bool
type ErrorEncoder func(ctx context.Context, err error, w http.ResponseWriter, r *http.Request) bool

// ErrorHandler receives a transport error to be processed for diagnostic purposes.
// Usually this means logging the error.
Expand All @@ -85,7 +85,7 @@ func (h handler[Request, Response]) ServeHTTP(w http.ResponseWriter, r *http.Req
// Might be a client error (can be encoded, non-terminal)
// Might be a server error (terminal)

handled := h.encodeError(ctx, err, w)
handled := h.encodeError(ctx, err, w, r)
if !handled {
h.errorHandler.HandleContext(ctx, err)
}
Expand All @@ -98,7 +98,7 @@ func (h handler[Request, Response]) ServeHTTP(w http.ResponseWriter, r *http.Req
// Might be a client error (can be encoded, non-terminal)
// Might be a server error (terminal)

handled := h.encodeError(ctx, err, w)
handled := h.encodeError(ctx, err, w, r)
if !handled {
h.errorHandler.HandleContext(ctx, err)
}
Expand All @@ -114,9 +114,9 @@ func (h handler[Request, Response]) ServeHTTP(w http.ResponseWriter, r *http.Req
}
}

func (h handler[Request, Response]) encodeError(ctx context.Context, err error, w http.ResponseWriter) bool {
func (h handler[Request, Response]) encodeError(ctx context.Context, err error, w http.ResponseWriter, r *http.Request) bool {
for _, errorEncoder := range h.errorEncoders {
if errorEncoder(ctx, err, w) {
if errorEncoder(ctx, err, w, r) {
return true
}
}
Expand Down

0 comments on commit e394f7e

Please sign in to comment.