Skip to content
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

Merged
merged 1 commit into from
Dec 24, 2017

Conversation

Alexsey
Copy link
Contributor

@Alexsey Alexsey commented Dec 22, 2017

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 used

With this PR thrown objects would be converted to JSON

@codecov
Copy link

codecov bot commented Dec 22, 2017

Codecov Report

Merging #1113 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1113      +/-   ##
==========================================
+ Coverage   99.73%   99.73%   +<.01%     
==========================================
  Files           5        5              
  Lines         371      372       +1     
==========================================
+ Hits          370      371       +1     
  Misses          1        1
Impacted Files Coverage Δ
lib/context.js 96.96% <100%> (+0.09%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 53bea20...5c1c1c3. Read the comment docs.

@Alexsey Alexsey changed the title Error handling: on non-error throw try to stringify if error is an object Error handling: try to stringify non-error throw if it is an object Dec 22, 2017
lib/context.js Outdated
if (!(err instanceof Error)) {
try {
if (Object.prototype.toString.call(err) === '[object Object]') {
err = JSON.stringify(err);
Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Contributor Author

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 ifs and additional code! Consider a new version now

@Alexsey Alexsey force-pushed the nonErrorHandling branch 2 times, most recently from 04bc7e6 to 786ea3d Compare December 24, 2017 11:26
@Alexsey
Copy link
Contributor Author

Alexsey commented Dec 24, 2017

There is still one case that util.format doesn't handle well - Symbol. It would convert it to undefined and this is frustrating. I think I'll make a special treatment for Symbol case later today

@dead-horse
Copy link
Member

this is enough, throw a literal is an edge case, and throw a symbol is meaningless.

@dead-horse dead-horse merged commit 6baa411 into koajs:master Dec 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants