Repository navigation
Description
Introduction
Currently, it is possible to pass in custom error handlers to be able to handle errors in a customised way as needed in case middleware validator fails, which is nice.
nethttp-middleware/oapi_validate.go
Line 20 in 5324ea6
| type ErrorHandler func(w http.ResponseWriter, message string, statusCode int) |
What is the problem
It would have been nicer if the error handler function also had an option of receive the http request object as to construct more schematic errors and respond with that. This may need the request details and context.
I understand that this can be more specific to the consumer of the library (which is me right now!), but I was wondering if this flexibility can be added without breaking existing users.
Proposed Solution of the problem
The solution that I am trying to propose here is just a starting idea(and can have cons) and need not be exactly like this. This can require discussion in the community to be able to come with a different one or build on this.
One option would be to introduce an altogether new error handler with config function that accepts an handler config which is illustrated in the code below:
( Please excuse the namings, i just want to propose the idea )
// Options to customize request validation, openapi3filter specified options will be passed through.
type Options struct {
Options openapi3filter.Options
// INTRODUCE an ErrorHandler with config.
ErrorHandlerWithConfig ErrorHandlerWithConfig
ErrorHandler ErrorHandler
MultiErrorHandler MultiErrorHandler
// SilenceServersWarning allows silencing a warning for https://github.com/deepmap/oapi-codegen/issues/882 that reports when an OpenAPI spec has `spec.Servers != nil`
SilenceServersWarning bool
}
// INTRODUCE an ErrorHandlerConfig struct.
type ErrorHandlerConfig struct {
w http.ResponseWriter
r http.Request
message string
statusCode int
... // extensible to other probable fields
}
// INTRODUCE a new function error handler function.
type ErrorHandlerWithConfig func(config ErrorHandlerConfig)We can skip executingthe current error hander and execute ErrorHandlerWithConfig if it is not empty otherwise fallback to using the current one if provided.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
jamietanna commentedon Apr 24, 2025
Thanks all for the patience and suggestions - I'm looking at getting this tackled via #35 - please take a look at the signature and let me know thoughts
(This will be merged in the next couple of days, and anyone who's contributed suggestions will get a
Co-Authored-Byin this change, so even though it won't show as a "merged PR" from you, it'll definitely show as you having contributed to the work!)feat: add error handler with more configuration
rew! feat: add error handler with more configuration
feat: add error handler with more configuration
feat: add error handler with more configuration
jamietanna commentedon Apr 25, 2025
Closed by #35.