Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

fix: Pull Error's name from constructor, not Error itself #372

Merged
merged 1 commit into from
Sep 20, 2017

Conversation

kamilogorek
Copy link
Contributor

Fixes: #350

Copy link
Contributor

@benvinegar benvinegar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment.

lib/parsers.js Outdated
@@ -15,7 +15,7 @@ module.exports.parseText = function parseText(message, kwargs) {

module.exports.parseError = function parseError(err, kwargs, cb) {
utils.parseStack(err, function(frames) {
var name = err.name + '';
var name = err.constructor.name + '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it not be:

var name = (err.name || err.constructor.name) + '';

That way if someone sets a local name property, that is prioritized over going up the constructor chain?

Copy link
Contributor Author

@kamilogorek kamilogorek Sep 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If one won't set err.name manually, then it defaults to Error, which will cause the code to never make it through the OR operator. Therefore err.constructor.name will always be skipped in this scenario.

Copy link
Contributor

@benvinegar benvinegar Sep 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about:

var name = {}.hasOwnProperty.call(err, 'name') ?
  err.name : err.constructor.name;

My interest is in maintaining the behavior where someone has set err.name manually; I'd like this to keep working without them having to keep up to date w/ the changelog.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Updated and added additional tests covering this cases.

@kamilogorek kamilogorek force-pushed the error-name branch 2 times, most recently from 16110a7 to de24ed9 Compare September 19, 2017 10:05
@kamilogorek kamilogorek merged commit 443ef26 into master Sep 20, 2017
@kamilogorek kamilogorek deleted the error-name branch September 20, 2017 09:07
@@ -15,7 +15,8 @@ module.exports.parseText = function parseText(message, kwargs) {

module.exports.parseError = function parseError(err, kwargs, cb) {
utils.parseStack(err, function(frames) {
var name = err.name + '';
var name =
({}.hasOwnProperty.call(err, 'name') ? err.name : err.constructor.name) + '';
Copy link

@deefeloper deefeloper Sep 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use Object.prototype.hasOwnProperty.call() since you don't create a new object first

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out. However, it's such a micro-optimization, which won't make any difference ever (this object will be collected by GC anyway), that I'll just patch it while working on something else and won't create a separate PR for it. We also use {}. notation in other places, and I prefer code consistency over small performance gains :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants