-
-
Notifications
You must be signed in to change notification settings - Fork 444
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
Add support for error aggregation for request/response validation #259
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2ef4682
WIP for multi part errors
zlozano 2e0ac59
add fail fast option
zlozano 270f058
merge in upstream changes
zlozano f62b88b
complete multerrors for request/response validation; add tests
zlozano 3255cbc
move allocation; remove TODO
zlozano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the input! I am about to push up the rest of my changes, and I will add this in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the
Is
and theAs
.The
StatusCoder
is defined in theopenapi3filter
package, so including here would introduce an import cycle. I think this is fine for now since the rules used above for calculating the status code are not universal. Also, it looks like theStatusCoder
is only implemented by theValidationError
.I'm not super familiar with the ValidationError/ErrorEncoder, so I am not quite sure how it would look, but I think what would be needed is a way to convert a
MultiError
into aValidationError
(similar to what is done forSchemaError
s andRequestError
s. It seems like this work could be done separately/as a follow up to this since the error will get passed-through to the encoder, allowing the end user to control this behavior. Related to my previous comment, it's not clear how exactly we would calculate error precedence if there are many errors. So this may be the best course of action for now. I'll defer to those who have more experience with using this feature.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given go's preference for interfaces being defined where they are consumed it would have been acceptable to redefine
StatusCoder
within this package or use an anonymous interface like some of the core packages. Ex:if sc, ok := err.(interface{StatusCode() string}); ok {
It is fine to leave the
StatusCode
function out for now. I actually had it as a standalone function in the proof of concept that I was working on.I agree we should have a separate discussion about conversion to
ValidationError
. I had a hard time implementing my ownErrorEncoder
.