Skip to content

Commit 25cec2f

Browse files
authored
Add support for error aggregation for request/response validation (#259)
1 parent d9b54af commit 25cec2f

7 files changed

Lines changed: 420 additions & 39 deletions

File tree

openapi3/errors.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package openapi3
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
)
7+
8+
// MultiError is a collection of errors, intended for when
9+
// multiple issues need to be reported upstream
10+
type MultiError []error
11+
12+
func (me MultiError) Error() string {
13+
buff := &bytes.Buffer{}
14+
for _, e := range me {
15+
buff.WriteString(e.Error())
16+
buff.WriteString(" | ")
17+
}
18+
return buff.String()
19+
}
20+
21+
//Is allows you to determine if a generic error is in fact a MultiError using `errors.Is()`
22+
//It will also return true if any of the contained errors match target
23+
func (me MultiError) Is(target error) bool {
24+
if _, ok := target.(MultiError); ok {
25+
return true
26+
}
27+
for _, e := range me {
28+
if errors.Is(e, target) {
29+
return true
30+
}
31+
}
32+
return false
33+
}
34+
35+
//As allows you to use `errors.As()` to set target to the first error within the multi error that matches the target type
36+
func (me MultiError) As(target interface{}) bool {
37+
for _, e := range me {
38+
if errors.As(e, target) {
39+
return true
40+
}
41+
}
42+
return false
43+
}

0 commit comments

Comments
 (0)