Skip to content

feat: add error handler with more configuration #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 25, 2025
Merged

Conversation

jamietanna
Copy link
Member

@jamietanna jamietanna commented Apr 24, 2025

Historically, handling errors with the OpenAPI request validation
middleware has been fairly frustrating, as we don't provide information
around the error that occurred (only the message), the
context.Context that this request is for, what HTTP method + path it's
for, or anything else about the request.

As a means to much more greatly improve the experience for our users, we
can introduce a new ErrorHandlerWithOpts function which provides all this
information, and the ability to extend this in the future.

This is introduced in a backwards-compatible way, with a new function,
to avoid a breaking change, and to allow consumers to migrate over to
the new function.

We'll mark the existing function as deprecated, to indicate folks should
migrate over, but also note there is no hard requirement or deadline
associated.

This introduces a new helper method
performRequestValidationForErrorHandler that will be used alongside
the new performRequestValidationForErrorHandlerWithOpts for the new
function, as a means to deduplicate processing the error itself.

We want to make sure that our testable examples add validation for this
functionality, as well as indicating how a MultiError could be handled
(with added complexity).

With thanks to Per, Mike and MattiasMartens who have made tangible
efforts towards this in this repository, as well as many others in the
past who have worked on suggestions and improvements towards this.

Closes #11, #27.

Historically, handling errors with the OpenAPI request validation
middleware has been fairly frustrating, as we don't provide information
around the `error` that occurred (only the message), the
`context.Context` that this request is for, what HTTP method + path it's
for, or anything else about the request.

As a means to much more greatly improve the experience for our users, we
can introduce a new `ErrorHandlerWithOpts` function which provides all this
information, and the ability to extend this in the future.

This is introduced in a backwards-compatible way, with a new function,
to avoid a breaking change, and to allow consumers to migrate over to
the new function.

We'll mark the existing function as deprecated, to indicate folks should
migrate over, but also note there is no hard requirement or deadline
associated.

This introduces a new helper method
`performRequestValidationForErrorHandler` that will be used alongside
the new `performRequestValidationForErrorHandlerWithOpts` for the new
function, as a means to deduplicate processing the error itself.

We want to make sure that our testable examples add validation for this
functionality, as well as indicating how a `MultiError` could be handled
(with added complexity).

With thanks to Per, Mike and MattiasMartens who have made tangible
efforts towards this in this repository, as well as many others in the
past who have worked on suggestions and improvements towards this.

Closes #11, #27.

Co-authored-by: Per Bockman <[email protected]>
Co-authored-by: Mike Schinkel <[email protected]>
Co-authored-by: MattiasMartens <[email protected]>
@jamietanna jamietanna marked this pull request as ready for review April 24, 2025 15:44
@jamietanna jamietanna requested a review from a team as a code owner April 24, 2025 15:44
@jamietanna jamietanna added the enhancement New feature or request label Apr 24, 2025
@mikeschinkel
Copy link
Contributor

@jamietanna — Thanks for tackling this.

One thing I notice is your ErrorHandlerOpts struct does not contain a context.Context that #14 was asking for. Was that an accidental or intentional omission?

// If both an `ErrorHandlerWithOpts` and `ErrorHandler` are set, the `ErrorHandlerWithOpts` takes precedence.
//
// NOTE that this should ideally be used instead of ErrorHandler
type ErrorHandlerWithOpts func(ctx context.Context, w http.ResponseWriter, r *http.Request, opts ErrorHandlerOpts)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a context.Context here - I thought it'd make more sense as a function parameter (and the first) than in the opts - thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jamietanna — As it has been a while since I was working on the project whose use-case caused me to write a PR the specifics faded from memory, and I commented because I was vaguely thinking there would two different contexts. However, in reviewing your reply and your code I seems I was just not thinking it through clearly enough and now see that your PR is probably the better and more idiomatic approach.

So, all good.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, no worries! No, I very much appreciate your review - I think if used with i.e. Gin, you may want the gin.Context and the context.Context, but in this case for pure net/http, we won't have that exposed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jamietanna — Hey, though. Super excited to see you address this as I am sure I will need it in the future.

@jamietanna jamietanna merged commit 124a344 into main Apr 25, 2025
26 checks passed
@jamietanna jamietanna deleted the feat/err-ctx branch April 25, 2025 06:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

Add context to ErrorHandler
2 participants