-
Notifications
You must be signed in to change notification settings - Fork 982
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
feat: adds support to http problem details - rfc7807 #1786
base: master
Are you sure you want to change the base?
Conversation
adds supports to handle error objects comforming with the http problem details spec (RFC7807). the spec defines a `status` field instead of `statusCode` and a `application/problem+json` content type.
@@ -1435,7 +1435,7 @@ Server.prototype._routeErrorResponse = function _routeErrorResponse( | |||
} | |||
|
|||
// only automatically send errors that are known (e.g., restify-errors) | |||
if (err instanceof Error && _.isNumber(err.statusCode)) { | |||
if (err instanceof Error && _.isNumber(err.statusCode || err.status)) { |
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.
when is this status
? I
restify-errors
does statusCode
https://github.com/restify/errors/blob/master/lib/baseClasses/HttpError.js#L54
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.
exactly, and restify-errors
doesn't conform to rfc7807, which defines status
. The problem is that it assumes that users are indeed using restify-errors
and don't give them the ability to follow the standard.
This PR keeps statusCode
for backward compatibility with restify-errors
but allows restify to return the correct content type application/problem+json
when status
is used and no statusCode
is present
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.
class NotFound extends Error {
constructor(detail) {
this.title = 'Not Found';
this.type = 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404';
this.status = 404;
this.detail = detail || 'Resource not found';
}
toJSON() {
return this;
}
}
this will conform with rfc7807 and will hit that code with status
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.
You know what? It doesn't really need to "have a status property instead of a statusCode" to conform to the rfc. I was mainly using status
property as a way to identify that was an error message complying with the rfc and sending the correct content-type.
But we can have a statusCode
property in the error object and on the toJSON
method that I serialize it to status
instead, but I would need to find a way to set the application/problem+json
content type and still keep backward compatibility. Any thoughts on this?
@@ -389,7 +389,7 @@ function patch(Response) { | |||
// If the body is an error object and we were not given a status code, | |||
// try to derive it from the error object, otherwise default to 500 | |||
if (!code && body instanceof Error) { | |||
code = body.statusCode || 500; | |||
code = body.statusCode || body.status || 500; |
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.
when is this status
?
Pre-Submission Checklist
make prepush
Changes
adds supports to handle error objects conforming with the http
problem details spec (rfc7807).
The spec defines a
status
field (instead ofstatusCode
) and aapplication/problem+json
content type. Code will prefer the currentbehavior of sending
application/json
ifstatusCode
is present.