@@ -13,51 +13,17 @@ import (
1313
1414const httpStatusCodeErrorAttribute = "openmeter.http.status_code"
1515
16- type v3ErrorMapping struct {
17- match func (err error ) bool
18- build func (ctx context.Context , err error ) * BaseAPIError
16+ type ErrorMapping struct {
17+ Match func (err error ) bool
18+ Build func (ctx context.Context , err error ) * BaseAPIError
1919}
2020
2121// NewV3ErrorHandlerFunc returns an oapi-codegen ChiServerOptions.ErrorHandlerFunc implementation.
2222//
2323// It is invoked when the generated router fails request binding (query/path/header parsing).
2424// The main purpose is to ensure we always write a response (otherwise net/http defaults to 200 with
2525// an empty body), and to keep error-to-status mapping consistent with our model error types.
26- func NewV3ErrorHandlerFunc (logger errorsx.Handler ) func (w http.ResponseWriter , r * http.Request , err error ) {
27- // Mirrors pkg/framework/commonhttp/encoder.go GenericErrorEncoder ordering (after the status-code attribute mapping).
28- mappings := []v3ErrorMapping {
29- {
30- match : models .IsGenericConflictError ,
31- build : func (ctx context.Context , err error ) * BaseAPIError { return NewConflictError (ctx , err , err .Error ()) },
32- },
33- {
34- match : models .IsGenericForbiddenError ,
35- build : func (ctx context.Context , err error ) * BaseAPIError { return NewForbiddenError (ctx , err ) },
36- },
37- {
38- match : models .IsGenericNotImplementedError ,
39- build : func (ctx context.Context , err error ) * BaseAPIError { return NewNotImplementedError (ctx , err ) },
40- },
41- {
42- match : models .IsGenericValidationError ,
43- build : func (ctx context.Context , err error ) * BaseAPIError { return NewBadRequestError (ctx , err , nil ) },
44- },
45- {
46- match : models .IsGenericNotFoundError ,
47- build : func (ctx context.Context , err error ) * BaseAPIError { return NewNotFoundError (ctx , err , "" ) },
48- },
49- {
50- match : models .IsGenericUnauthorizedError ,
51- build : func (ctx context.Context , err error ) * BaseAPIError { return NewUnauthenticatedError (ctx , err ) },
52- },
53- {
54- match : models .IsGenericPreConditionFailedError ,
55- build : func (ctx context.Context , err error ) * BaseAPIError {
56- return NewPreconditionFailedError (ctx , err .Error ())
57- },
58- },
59- }
60-
26+ func NewV3ErrorHandlerFunc (logger errorsx.Handler , mappings []ErrorMapping ) func (w http.ResponseWriter , r * http.Request , err error ) {
6127 return func (w http.ResponseWriter , r * http.Request , err error ) {
6228 if err == nil {
6329 return
@@ -90,9 +56,9 @@ func NewV3ErrorHandlerFunc(logger errorsx.Handler) func(w http.ResponseWriter, r
9056 }
9157
9258 for _ , m := range mappings {
93- if m .match (err ) {
59+ if m .Match (err ) {
9460 logger .HandleContext (ctx , err )
95- m .build (ctx , err ).HandleAPIError (w , r )
61+ m .Build (ctx , err ).HandleAPIError (w , r )
9662 return
9763 }
9864 }
0 commit comments