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

Fix #422 added support for error unwrapping for errors with a single sub-error #433

Merged
merged 2 commits into from
Oct 7, 2021
Merged
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
6 changes: 6 additions & 0 deletions openapi3/schema.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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