-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Remove code from error name #1786
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
Conversation
Could you reference to this behaviour in code or doc? I couldn't find anything. I disagree with that change because the
I don't understand it. Could you elaborate? |
|
Therefore you example is only valid for node core issues. In userland errors you will get this! No error code! const e = new Error('#######')
e.code = "dede"
throw e
Error: #######
at Object.<anonymous> (/mnt/ssd/repositories/rbtx/rbtx-web/test.js:2:17)
at Module._compile (internal/modules/cjs/loader.js:816:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
at Module.load (internal/modules/cjs/loader.js:685:32)
at Function.Module._load (internal/modules/cjs/loader.js:620:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:877:12)
at internal/main/run_main_module.js:21:11Only if you print the err object you will see the code at the end of the stack trace try {
const e = new Error('#######')
e.code = "dede"
throw e
} catch (error) {
console.log(error)
console.log('-----------')
console.log(error.name)
console.log(error.message)
console.log(error.code)
}
{ Error: #######
at Object.<anonymous> (/mnt/ssd/repositories/rbtx/rbtx-web/test.js:2:17)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3) code: 'dede' }Do I miss smt? |
|
People parse errors and expect the The simple fact is: |
Which people? Issues? I just expect a useful name and the error code will help to identify it. If you need a constant name you could rely on the constructor name. I have never heard that the error name must be a fixed across all errors. |
|
https://www.npmjs.com/package/error-stack-parser I could list packages that parse errors all day long. This PR does not remove the error code. The error code is still there to provide the utility it is meant to provide: insight into the specific situation that caused the error. |
|
I have no strong opinion about that change but I like it as it is because it's easy too identify. How is the error logged in fastify? (can't test right now) Is the fastify error code printed? When yes +1 |
| t.plan(1) | ||
| const NewError = createError('CODE', 'foo') | ||
| const err = new NewError() | ||
| t.equal(err.toString(), 'FastifyError [CODE]: foo') |
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.
Does the default error serializer rely on toString?
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.
Yes.
StarpTech
left a comment
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.
LGTM
mcollina
left a comment
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.
LGTM
|
This is tagged as semver-major, but is that so? Errors are identified by code, so that we can change the name without breakage. |
allevo
left a comment
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.
LGTM
mcollina
left a comment
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.
LGTM
|
I wrote it against What do you say @mcollina? |
Add FastifyError.toString to display code in `console.log(error)`
5fd7d66 to
54c2b78
Compare
|
See #1787 for this PR against |
delvedor
left a comment
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.
LGTM
Add FastifyError.toString to display code in `console.log(error)`
|
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. |
Run the following under Node 12:
You will get the following output:
Notice that
error.namedoes not includeerror.code. Node's default error serialization automatically inserts the code into the message. There isn't any need forfastify/lib/errors.js
Line 81 in 53bac6d
^ That just makes it more difficult for downstream users to work with the error objects. This PR removes the code from the names of
FastifyErrorinstances.Checklist
npm run testandnpm run benchmark