3 ms·
Why the suggestion to use an error's name rather then instaneof and the error's class?
by Illniyar 10y ago
Why the suggestion to use an error's name rather then instaneof and the error's class?
- prodigal_erik 10y agoError.prototype.toString() reads e.name, not e.prototype.constructor.name, so you can't rely on everyone to have subclassed Error. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/name https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- Illniyar 10y agoI'm not following, why can't I use: e instanceof Error or: e instanceof MyError why does toString() have anything to do with this?
- pags 10y agoThis is what I do when working with my own Error types.
- prodigal_erik 10y agoIt's very likely someone did const e = new Error('bad stuff happened') e.name = 'MyError' without actually creating a MyError class to check with instanceof.
- Illniyar 10y agoUnless it's common in popular libraries/packages, I don't see why I need to take it into account. Which popular libraries do this? If it's just in a few places, it should be handled specifically, and use sane choices in other places.