Skip to content

Commit 5a162f6

Browse files
authored
Fix #422 added support for error unwrapping for errors with a single sub-error (#433)
1 parent 47bb0b2 commit 5a162f6

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

openapi3/schema.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,8 @@ type SchemaError struct {
14891489
Origin error
14901490
}
14911491

1492+
var _ interface{ Unwrap() error } = SchemaError{}
1493+
14921494
func markSchemaErrorKey(err error, key string) error {
14931495
if v, ok := err.(*SchemaError); ok {
14941496
v.reversePath = append(v.reversePath, key)
@@ -1564,6 +1566,10 @@ func (err *SchemaError) Error() string {
15641566
return buf.String()
15651567
}
15661568

1569+
func (err SchemaError) Unwrap() error {
1570+
return err.Origin
1571+
}
1572+
15671573
func isSliceOfUniqueItems(xs []interface{}) bool {
15681574
s := len(xs)
15691575
m := make(map[string]struct{}, s)

openapi3filter/errors.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ type RequestError struct {
1717
Err error
1818
}
1919

20+
var _ interface{ Unwrap() error } = RequestError{}
21+
2022
func (err *RequestError) Error() string {
2123
reason := err.Reason
2224
if e := err.Err; e != nil {
@@ -35,6 +37,10 @@ func (err *RequestError) Error() string {
3537
}
3638
}
3739

40+
func (err RequestError) Unwrap() error {
41+
return err.Err
42+
}
43+
3844
var _ error = &ResponseError{}
3945

4046
// ResponseError is returned by ValidateResponse when response does not match OpenAPI spec
@@ -44,6 +50,8 @@ type ResponseError struct {
4450
Err error
4551
}
4652

53+
var _ interface{ Unwrap() error } = ResponseError{}
54+
4755
func (err *ResponseError) Error() string {
4856
reason := err.Reason
4957
if e := err.Err; e != nil {
@@ -56,6 +64,10 @@ func (err *ResponseError) Error() string {
5664
return reason
5765
}
5866

67+
func (err ResponseError) Unwrap() error {
68+
return err.Err
69+
}
70+
5971
var _ error = &SecurityRequirementsError{}
6072

6173
// SecurityRequirementsError is returned by ValidateSecurityRequirements

openapi3filter/req_resp_decoder.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7-
"gopkg.in/yaml.v2"
87
"io"
98
"io/ioutil"
109
"mime"
@@ -15,6 +14,8 @@ import (
1514
"strconv"
1615
"strings"
1716

17+
"gopkg.in/yaml.v2"
18+
1819
"github.com/getkin/kin-openapi/openapi3"
1920
)
2021

@@ -42,6 +43,8 @@ type ParseError struct {
4243
path []interface{}
4344
}
4445

46+
var _ interface{ Unwrap() error } = ParseError{}
47+
4548
func (e *ParseError) Error() string {
4649
var msg []string
4750
if p := e.Path(); len(p) > 0 {
@@ -81,6 +84,10 @@ func (e *ParseError) RootCause() error {
8184
return e.Cause
8285
}
8386

87+
func (e ParseError) Unwrap() error {
88+
return e.Cause
89+
}
90+
8491
// Path returns a path to the root cause.
8592
func (e *ParseError) Path() []interface{} {
8693
var path []interface{}

0 commit comments

Comments
 (0)