Skip to content

Commit 103fa1e

Browse files
author
Jamie Tanna
committed
sq
1 parent 44b5e0c commit 103fa1e

1 file changed

Lines changed: 41 additions & 15 deletions

File tree

oapi_validate_example_test.go

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -705,14 +705,33 @@ paths:
705705
switch e := err.(type) {
706706
// NOTE that when it's a MultiError, there's more work needed here
707707
case openapi3.MultiError:
708-
for _, eee := range e {
709-
fmt.Printf("eee: %v\n", eee)
708+
var re *openapi3filter.RequestError
709+
if e.As(&re) {
710+
out := fmt.Sprintf("A MultiError was encountered, which contained a RequestError: %s", re)
711+
712+
if re.Err != nil {
713+
out += ", which inside it has a error of type (" + reflect.TypeOf(e).String() + ")"
714+
}
715+
716+
fmt.Printf("ErrorHandlerWithOpts: %s\n", out)
717+
718+
http.Error(w, "There was a bad request", opts.StatusCode)
719+
return
720+
}
721+
722+
var se *openapi3filter.SecurityRequirementsError
723+
if e.As(&se) {
724+
out := fmt.Sprintf("A MultiError was encountered, which contained a SecurityRequirementsError: %s", re)
725+
726+
if len(se.Errors) > 0 {
727+
out += fmt.Sprintf(", which contains %d child errors", len(se.Errors))
728+
}
729+
730+
fmt.Printf("ErrorHandlerWithOpts: %s\n", out)
731+
732+
http.Error(w, "There was an unauthorized request", opts.StatusCode)
733+
return
710734
}
711-
http.Error(w, fmt.Sprintf("There were %d errors with that request - try again?", len(e)), opts.StatusCode)
712-
return
713-
// TODO
714-
// http.Error(w, "MULTI"+errors.Join(e).Error(), opts.StatusCode)
715-
// return
716735
}
717736

718737
http.Error(w, err.Error(), opts.StatusCode)
@@ -770,14 +789,12 @@ paths:
770789

771790
// Output:
772791
// # A request that is malformed is rejected with HTTP 400 Bad Request (with no request body), and is then logged by the ErrorHandlerWithOpts
773-
// ErrorHandlerWithOpts: A RequestError was returned when attempting to validate the request to POST /resource: request body has an error: value is required but missing
774-
// This operation has a request body, which was required
775-
// There was a child error, which was an unknown type (*errors.errorString)
792+
// ErrorHandlerWithOpts: A MultiError was encountered, which contained a RequestError: request body has an error: value is required but missing, which inside it has a error of type (openapi3.MultiError)
776793
// Received an HTTP 400 response. Expected HTTP 400
777-
// Response body: A bad request was made - but I'm not going to tell you where or how
794+
// Response body: There was a bad request
778795
//
779796
// # A request that is malformed is rejected with HTTP 400 Bad Request (with an invalid request body, with multiple issues), and is then logged by the ErrorHandlerWithOpts
780-
// ErrorHandlerWithOpts: A RequestError was returned when attempting to validate the request to POST /resource: request body has an error: doesn't match schema: Error at "/id": minimum string length is 100
797+
// ErrorHandlerWithOpts: A MultiError was encountered, which contained a RequestError: request body has an error: doesn't match schema: Error at "/id": minimum string length is 100
781798
// Schema:
782799
// {
783800
// "minLength": 100,
@@ -786,9 +803,18 @@ paths:
786803
//
787804
// Value:
788805
// "not-long-enough"
806+
// | Error at "/name": value is not one of the allowed values ["Marcin"]
807+
// Schema:
808+
// {
809+
// "enum": [
810+
// "Marcin"
811+
// ],
812+
// "type": "string"
813+
// }
789814
//
790-
// This operation has a request body, which was required
791-
// There was a child error, which was a SchemaError, which failed to validate on the minLength field
815+
// Value:
816+
// "Jamie"
817+
// , which inside it has a error of type (openapi3.MultiError)
792818
// Received an HTTP 400 response. Expected HTTP 400
793-
// Response body: A bad request was made - but I'm not going to tell you where or how
819+
// Response body: There was a bad request
794820
}

0 commit comments

Comments
 (0)