Skip to content

Commit

Permalink
Fix #422 added support for error unwrapping for errors with a single …
Browse files Browse the repository at this point in the history
…sub-error (#433)
  • Loading branch information
bionoren committed Oct 7, 2021
1 parent 47bb0b2 commit 5a162f6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
6 changes: 6 additions & 0 deletions openapi3/schema.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions openapi3filter/errors.go
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down
9 changes: 8 additions & 1 deletion openapi3filter/req_resp_decoder.go
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"gopkg.in/yaml.v2"
"io"
"io/ioutil"
"mime"
Expand All @@ -15,6 +14,8 @@ import (
"strconv"
"strings"

"gopkg.in/yaml.v2"

"github.com/getkin/kin-openapi/openapi3"
)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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{}
Expand Down

0 comments on commit 5a162f6

Please sign in to comment.