Skip to content
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

feat!: add context param to error handler func #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/test/chi/oapi_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func TestOapiRequestValidatorWithOptions(t *testing.T) {
// Set up an authenticator to check authenticated function. It will allow
// access to "someScope", but disallow others.
options := middleware.Options{
ErrorHandler: func(w http.ResponseWriter, message string, statusCode int) {
ErrorHandler: func(ctx context.Context, w http.ResponseWriter, message string, statusCode int) {
http.Error(w, "test: "+message, statusCode)
},
Options: openapi3filter.Options{
Expand Down
4 changes: 2 additions & 2 deletions internal/test/gorilla/oapi_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"net/url"
"testing"

"github.com/oapi-codegen/testutil"
middleware "github.com/oapi-codegen/nethttp-middleware"
"github.com/oapi-codegen/testutil"

"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3filter"
Expand Down Expand Up @@ -266,7 +266,7 @@ func TestOapiRequestValidatorWithOptions(t *testing.T) {
// Set up an authenticator to check authenticated function. It will allow
// access to "someScope", but disallow others.
options := middleware.Options{
ErrorHandler: func(w http.ResponseWriter, message string, statusCode int) {
ErrorHandler: func(ctx context.Context, w http.ResponseWriter, message string, statusCode int) {
http.Error(w, "test: "+message, statusCode)
},
Options: openapi3filter.Options{
Expand Down
4 changes: 2 additions & 2 deletions internal/test/nethttp/oapi_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"net/url"
"testing"

"github.com/oapi-codegen/testutil"
middleware "github.com/oapi-codegen/nethttp-middleware"
"github.com/oapi-codegen/testutil"

"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3filter"
Expand Down Expand Up @@ -281,7 +281,7 @@ func TestOapiRequestValidatorWithOptions(t *testing.T) {
// Set up an authenticator to check authenticated function. It will allow
// access to "someScope", but disallow others.
options := middleware.Options{
ErrorHandler: func(w http.ResponseWriter, message string, statusCode int) {
ErrorHandler: func(ctx context.Context, w http.ResponseWriter, message string, statusCode int) {
http.Error(w, "test: "+message, statusCode)
},
Options: openapi3filter.Options{
Expand Down
5 changes: 3 additions & 2 deletions oapi_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package nethttpmiddleware

import (
"context"
"errors"
"fmt"
"log"
Expand All @@ -17,7 +18,7 @@ import (
)

// ErrorHandler is called when there is an error in validation
type ErrorHandler func(w http.ResponseWriter, message string, statusCode int)
type ErrorHandler func(ctx context.Context, w http.ResponseWriter, message string, statusCode int)

// MultiErrorHandler is called when oapi returns a MultiError type
type MultiErrorHandler func(openapi3.MultiError) (int, error)
Expand Down Expand Up @@ -53,7 +54,7 @@ func OapiRequestValidatorWithOptions(swagger *openapi3.T, options *Options) func
// validate request
if statusCode, err := validateRequest(r, router, options); err != nil {
if options != nil && options.ErrorHandler != nil {
options.ErrorHandler(w, err.Error(), statusCode)
options.ErrorHandler(r.Context(), w, err.Error(), statusCode)
} else {
http.Error(w, err.Error(), statusCode)
}
Expand Down