Skip to content

Commit

Permalink
Add support for error aggregation for request/response validation (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
zlozano authored Oct 26, 2020
1 parent d9b54af commit 25cec2f
Show file tree
Hide file tree
Showing 7 changed files with 420 additions and 39 deletions.
43 changes: 43 additions & 0 deletions openapi3/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package openapi3

import (
"bytes"
"errors"
)

// MultiError is a collection of errors, intended for when
// multiple issues need to be reported upstream
type MultiError []error

func (me MultiError) Error() string {
buff := &bytes.Buffer{}
for _, e := range me {
buff.WriteString(e.Error())
buff.WriteString(" | ")
}
return buff.String()
}

//Is allows you to determine if a generic error is in fact a MultiError using `errors.Is()`
//It will also return true if any of the contained errors match target
func (me MultiError) Is(target error) bool {
if _, ok := target.(MultiError); ok {
return true
}
for _, e := range me {
if errors.Is(e, target) {
return true
}
}
return false
}

//As allows you to use `errors.As()` to set target to the first error within the multi error that matches the target type
func (me MultiError) As(target interface{}) bool {
for _, e := range me {
if errors.As(e, target) {
return true
}
}
return false
}
Loading

0 comments on commit 25cec2f

Please sign in to comment.