Skip to content

v1.1.0: Better error handling, allow not validating `Servers` (by configuration) and return an HTTP 405 Method Not Allowed where appropriate

Choose a tag to compare

@github-actions github-actions released this 25 Apr 07:55
· 6 commits to refs/heads/main since this release

πŸ”Š Notable features

Error handling is now significantly better

As part of #35, we have introduced a much more powerful means to handle the errors returned by the OpenAPI Validation middleware.

This has been a long-standing issue, and we appreciate the community's patience as well as different proposals over time.

Previously, when creating an error handler, the method signature was fairly lacking:

// create middleware
mw := middleware.OapiRequestValidatorWithOptions(spec, &middleware.Options{
    Options: openapi3filter.Options{
        AuthenticationFunc: authenticationFunc,
    },
    ErrorHandler: func(w http.ResponseWriter, message string, statusCode int) {
        http.Error(w, "This text/plain response will be returned to the caller: " + message, statusCode)
    },
})

This didn't give much information around what had happened - or in which route/endpoint - and not having the raw error made observability and additional steps to respond appropriately more difficult.

With this release, it's now possible to have the raw error, information about the current request + its context.Context, as well as some additional pieces of metadata inside the ErrorHandlerWithOpts:

// create middleware
mw := middleware.OapiRequestValidatorWithOptions(spec, &middleware.Options{
    // other options
    // ...
    ErrorHandlerWithOpts: func(ctx context.Context, err error, w http.ResponseWriter, r *http.Request, opts middleware.ErrorHandlerOpts) {
        // NOTE that ErrorHandlerOpts contains further information, and allows us to add additional information in the future, in an extensible and non-breaking way
    }
})

There is a full example of usage in the Go doc for this project.

Note that the old method, ErrorHandler has been marked as deprecated - there is no planned removal date, and this is largely as a way to "nudge" folks to using the new, more powerful, method.

πŸš€ New features and improvements

πŸ› Bug fixes

  • fix: return HTTP 405 when request method mismatch (#25) @nek023

πŸ“ Documentation updates

πŸ‘» Maintenance

πŸ“¦ Dependency updates

Sponsors

We would like to thank our sponsors for their support during this release.

Elastic logo

DevZero logo

Speakeasy logo

Cybozu logo

Livepeer logo