(no title)
pokoleo | 4 years ago
The example uses a different code per issue, for instance: "user/email_required". Most integrators will build their UI to highlight the input fields that contain an error. Making them parse the `code` field (or special-case each possible code) is pretty toilsome.
// from blog post
{
"code": "user/email_required",
"message": "The parameter [email] is required."
}
Make it parseable: // improved
{
"message": "An email is required.",
"error": "missing_parameter",
"parameter": "user.email"
}
In addition, I:* rewrote `message` to be an acceptable error message displayed to (non-technical) end-users
* moved message to be the first field: some developer tools will truncate the JSON response body when presenting it in the stack trace.
---
As an added bonus, structured data allows you to analyze `error` frequencies and improve frontend validation or write better error messages: https://twitter.com/VicVijayakumar/status/149509216182142976...
pan69|4 years ago
https://datatracker.ietf.org/doc/html/rfc7807
aeontech|4 years ago
I’d love to hear from anyone who has encountered APIs in the wild that actually implement this standard!
AtNightWeCode|4 years ago
b-pagis|4 years ago
For more complex use cases, where we would want an error message to indicate that field value was too long and in addition provide maximum field length, we would need to introduce new field in the error response.
While it is solvable by adding this information to client application side. It would create a situation where the logic is duplicated in two places (backend and client application)...
Also if we would want better UX, then we would need to display all errors at the same time in the form that is incorrectly filled. This would require changing error structure to return array of errors and it potentially create a breaking change in the API or would result in confusing structure that supports both, legacy and new format...
Some years ago, I wrote an article sharing the ideas on how REST API error structuring could be done depending on the complexity of application or service: https://link.medium.com/ObW78jhDkob
pokoleo|4 years ago
Most validation I’ve seen looks like:
This pattern turns into one exception per response, and some foresight in architecting exceptions would be neededhinkley|4 years ago
You need codes because the field isn't going to be 'email' for much longer than it takes for your management to realize that people outside of the US also have wallets.
codys|4 years ago
(While the comment I replied to can be read differently, I assume we all know that changing actual field names (in APIs) depending on localization is nuts)
aidos|4 years ago
chaz6|4 years ago
moltar|4 years ago
https://jsonapi.org/format/
(Scroll to the very bottom)
pokoleo|4 years ago
smoyer|4 years ago
rbluethl|4 years ago