-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Error handling: try to stringify non-error throw if it is an object #1113
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1113 +/- ##
==========================================
+ Coverage 99.73% 99.73% +<.01%
==========================================
Files 5 5
Lines 371 372 +1
==========================================
+ Hits 370 371 +1
Misses 1 1
Continue to review full report at Codecov.
|
lib/context.js
Outdated
if (!(err instanceof Error)) { | ||
try { | ||
if (Object.prototype.toString.call(err) === '[object Object]') { | ||
err = JSON.stringify(err); |
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.
err must convert to Error instance, BTW can we use new Error(util.format('non-error thrown: %j', err)
here?
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.
What is the reason to convert it to an error if on the next line it would be converted to a string with ${err}
? And generally there is no sense in the conversion because stack trace is already lost - new Error would have a stack trace to Koa internals which is not helpful :( Although usage of util.format
may be a good idea - I'll try to play with it to make a code nicer
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.
it will emit in app
here, we must keep err
to be an instance of Error so it won't break anyone's code.
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.
Hm. But it would already always become an error two lines lower
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 where right about util.format
- it really covers all this cases without if
s and additional code! Consider a new version now
04bc7e6
to
786ea3d
Compare
786ea3d
to
5c1c1c3
Compare
There is still one case that |
this is enough, throw a literal is an edge case, and throw a symbol is meaningless. |
In case when object were thrown error message is really non helpful - you just see
non-error thrown: [object Object]
and stack trace is leads to Koa source. If someone throws an object - it is his fault that no stack trace would be provided, but seen the object itself is already a good start. A specially when it was not your code but some of the modules that you usedWith this PR thrown objects would be converted to JSON