Skip to content

More flexible ErrorHandler function to plugin in oapi validator middleware #27

Closed

Description

@sonasingh46

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.

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.

Activity

  1. jamietanna commented on Apr 24, 2025

    @jamietanna
    Member

    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-By in 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!)

  2. jamietanna commented on Apr 25, 2025

    @jamietanna
    Member

    Closed by #35.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions