Hello there! 👋🏻
When passing an invalid enum value to a JSON field using the enum type an untyped / unwrapped error is thrown:
|
return 0, fmt.Errorf("%%s does not belong to %[1]s values", s) |
This makes distinguishing harder between an actual 400 response (body is not parsable) and a 422 (the data you passed are wrong) response.
Would you be up to introducing a typed error, that can be identified using errors.Is() and eventually accepting a PR for that?
As of right now, as a workaround, I am parsing the error string while setting the response status code as follows:
status := 400
enumerErrorStringMatcher := regexp.MustCompile(`^"\w+ does not belong to \w+ values"$`)
if enumerErrorStringMatcher.MatchString(err.Error()) {
status = http.StatusUnprocessableEntity
}
Cheers,
Alex