From 5a162f64354ef69fb4b52e4343f45bd224bf6f03 Mon Sep 17 00:00:00 2001 From: Bion <520596+bionoren@users.noreply.github.com> Date: Thu, 7 Oct 2021 07:10:06 -0500 Subject: [PATCH] Fix #422 added support for error unwrapping for errors with a single sub-error (#433) --- openapi3/schema.go | 6 ++++++ openapi3filter/errors.go | 12 ++++++++++++ openapi3filter/req_resp_decoder.go | 9 ++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/openapi3/schema.go b/openapi3/schema.go index 69aee8d7c..de3e5167d 100644 --- a/openapi3/schema.go +++ b/openapi3/schema.go @@ -1489,6 +1489,8 @@ type SchemaError struct { Origin error } +var _ interface{ Unwrap() error } = SchemaError{} + func markSchemaErrorKey(err error, key string) error { if v, ok := err.(*SchemaError); ok { v.reversePath = append(v.reversePath, key) @@ -1564,6 +1566,10 @@ func (err *SchemaError) Error() string { return buf.String() } +func (err SchemaError) Unwrap() error { + return err.Origin +} + func isSliceOfUniqueItems(xs []interface{}) bool { s := len(xs) m := make(map[string]struct{}, s) diff --git a/openapi3filter/errors.go b/openapi3filter/errors.go index ec8ea053c..8454c817f 100644 --- a/openapi3filter/errors.go +++ b/openapi3filter/errors.go @@ -17,6 +17,8 @@ type RequestError struct { Err error } +var _ interface{ Unwrap() error } = RequestError{} + func (err *RequestError) Error() string { reason := err.Reason if e := err.Err; e != nil { @@ -35,6 +37,10 @@ func (err *RequestError) Error() string { } } +func (err RequestError) Unwrap() error { + return err.Err +} + var _ error = &ResponseError{} // ResponseError is returned by ValidateResponse when response does not match OpenAPI spec @@ -44,6 +50,8 @@ type ResponseError struct { Err error } +var _ interface{ Unwrap() error } = ResponseError{} + func (err *ResponseError) Error() string { reason := err.Reason if e := err.Err; e != nil { @@ -56,6 +64,10 @@ func (err *ResponseError) Error() string { return reason } +func (err ResponseError) Unwrap() error { + return err.Err +} + var _ error = &SecurityRequirementsError{} // SecurityRequirementsError is returned by ValidateSecurityRequirements diff --git a/openapi3filter/req_resp_decoder.go b/openapi3filter/req_resp_decoder.go index 2dc392864..cb58b62b6 100644 --- a/openapi3filter/req_resp_decoder.go +++ b/openapi3filter/req_resp_decoder.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "fmt" - "gopkg.in/yaml.v2" "io" "io/ioutil" "mime" @@ -15,6 +14,8 @@ import ( "strconv" "strings" + "gopkg.in/yaml.v2" + "github.com/getkin/kin-openapi/openapi3" ) @@ -42,6 +43,8 @@ type ParseError struct { path []interface{} } +var _ interface{ Unwrap() error } = ParseError{} + func (e *ParseError) Error() string { var msg []string if p := e.Path(); len(p) > 0 { @@ -81,6 +84,10 @@ func (e *ParseError) RootCause() error { return e.Cause } +func (e ParseError) Unwrap() error { + return e.Cause +} + // Path returns a path to the root cause. func (e *ParseError) Path() []interface{} { var path []interface{}