-
-
Notifications
You must be signed in to change notification settings - Fork 20
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
implement RFC7807 #65
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
const { inherits, format } = require('util') | ||
|
||
function createError (code, message, statusCode = 500, Base = Error) { | ||
function createError (code, message, statusCode = 500, Base = Error, uriReference) { | ||
if (!code) throw new Error('Fastify error code must not be empty') | ||
if (!message) throw new Error('Fastify error message must not be empty') | ||
|
||
|
@@ -28,13 +28,26 @@ function createError (code, message, statusCode = 500, Base = Error) { | |
} | ||
|
||
this.statusCode = statusCode || undefined | ||
this.uriReference = uriReference || undefined | ||
} | ||
FastifyError.prototype[Symbol.toStringTag] = 'Error' | ||
|
||
FastifyError.prototype.toString = function () { | ||
return `${this.name} [${this.code}]: ${this.message}` | ||
} | ||
|
||
FastifyError.prototype.toRFC7807 = function (instance, details) { | ||
return { | ||
type: this.uriReference || 'about:blank', | ||
title: this.name, | ||
status: this.statusCode, | ||
detail: this.message, | ||
instance: instance || '', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would be the place where the error in the fastify route happens. Like you have an ForbiddenError in GET /users/1234 then the instance would be |
||
code: this.code, | ||
details: details || {} | ||
} | ||
} | ||
|
||
inherits(FastifyError, Base) | ||
|
||
return FastifyError | ||
|
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 think the URI reference should be taken from the request itself and defined at the top for all errors of the same kind.
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 called it uriReference because according to RFC7807 it is called type. But that would be imho confusing.
https://datatracker.ietf.org/doc/html/rfc7807#section-3.1
So e.g. we create a FastifyError with code 'FST_ERR_BAD_URL' then the uriReference would be
https://www.fastify.io/docs/latest/Reference/Errors/#fst_err_bad_url
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.
Thats why this is an parameter of the createError function and not on the toRFC7807. On the toRFC7807 I use the instance parameter to provide uri from the request.
I wanted to ensure that we have no potential backwards breaking, but I could put instance and details as parameters of the actual Error. So toRFC7807 would just this.instance and this.details instead using them from toRFC7807.
Maybe that is better, as we would have those values at the time we throw the Error?