| package apierrors | |
| import ( | |
| "context" | |
| "net/http" | |
| "github.com/samber/lo" | |
| "github.com/openmeterio/openmeter/openmeter/meter" | |
| "github.com/openmeterio/openmeter/openmeter/productcatalog/feature" | |
| "github.com/openmeterio/openmeter/pkg/framework/commonhttp" | |
| "github.com/openmeterio/openmeter/pkg/framework/transport/httptransport/encoder" | |
| ) | |
| // GenericErrorEncoder is an error encoder that encodes the error as a generic error. | |
| func GenericErrorEncoder() encoder.ErrorEncoder { | |
| return func(ctx context.Context, err error, w http.ResponseWriter, r *http.Request) bool { | |
| if err, ok := lo.ErrorsAs[*BaseAPIError](err); ok { | |
| err.HandleAPIError(w, r) | |
| return true | |
| } | |
| return commonhttp.HandleErrorIfTypeMatches[*feature.FeatureNotFoundError](ctx, http.StatusNotFound, err, w) || | |
| commonhttp.HandleErrorIfTypeMatches[*meter.MeterNotFoundError](ctx, http.StatusNotFound, err, w) || | |
| commonhttp.HandleIssueIfHTTPStatusKnown(ctx, err, w) | |
| } | |
| } | |