Feature/#909 error codes#957
Conversation
|
This is good for me. It needs all errors to be copied and docs. |
allevo
left a comment
There was a problem hiding this comment.
Nice work! I like it.
Only a suggestion
| return `FastifyError [${code}]` | ||
| } | ||
| get code () { | ||
| return code |
There was a problem hiding this comment.
Can this property set to the prototype?
There was a problem hiding this comment.
What do you mean? It is part of the prototype. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get#get_Vs._defineProperty
There was a problem hiding this comment.
FastifyError.prototype.code = code
There was a problem hiding this comment.
allevo
left a comment
There was a problem hiding this comment.
Can you list all errors in the doc?
|
|
||
| function createMessage (msg, args = []) { | ||
| args.unshift(msg) | ||
| if (args.length === 0) return msg |
There was a problem hiding this comment.
This line should be before args.unshift(msg), otherwise args.length will always be at least 1.
| code = code.toUpperCase() | ||
| codes[code] = class FastifyError extends Base { | ||
| constructor (...args) { | ||
| super(`Code: ${code}; ${createMessage(msg, args)}`) |
There was a problem hiding this comment.
As an alternative to my comment above, you could use util.format() here directly. It already includes a check similar to the one you have in createMessage().
There was a problem hiding this comment.
Good addition. I will remove it.
|
|
||
| function createError (code, msg, statusCode = 500, Base = Error) { | ||
| if (!code) throw new Error('Fastify error code must not be empty') | ||
| if (!msg) throw new Error('Fastify error message must not be empty') |
There was a problem hiding this comment.
If you pass an empty string this will throw, is it ok?
Can you use more strict checks?
There was a problem hiding this comment.
I think that's enough. I just want to avoid that an empty value (0, '') or no value is passed.
delvedor
left a comment
There was a problem hiding this comment.
Great work!
Just a question, why are you using the class syntax?
Personally, I prefer to stick classic one, for two main reasons:
- in Fastify we aren’t using (es6) classes anywhere, while we are using JavaScript prototype-based inheritance (see request, reply…)
- class syntax is just syntactic sugar around the classic JavaScript prototype-based inheritance
Would the following code produce the same result?
const { inherits } = require('util')
function FastifyError (code, message) {
Error.call(this)
Error.captureStackTrace(this, FastifyError)
this.name = `FastifyError [${code}]`
this.code = code
this.message = message
}
inherits(FastifyError, Error)Remove eslint ignore comment
* Remove/Merge redundant decorate functions * Should return this instead of prototype * Simplify existence check
Add more details about the baseline ajv configuration
|
@StarpTech ping |
|
Hi @mcollina my time is very limited right now. I will try to continue my work on the weekend. Thanks for pointing me out. |
…tify#1162) * added default Query, Params, Headers, and Body types * fixed contentTypeParser types * added additional tests for default Buffer body parser * fixed tests * allow array of strings in addContentTypeParser
* added default Query, Params, Headers, and Body types * added tests * added tests for generics * added generics info to docs
* Added error handling doc * Updated based on comments * Update 2, based on comments * Updated based on comments
|
There is something off with this branch. You should probably rebase over next. |
|
I will create a new one. |
|
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
This is a first draft of introducing error codes to fastify. It only handles the codes for the contentTypeParser.
The old PR discussion is here
TODO
Checklist
npm run testandnpm run benchmark