mk-pmb / is-error-js

Detect whether a value is an error
MIT License
15 stars 1 forks source link

support toStringTag #5

Closed stevenvachon closed 4 years ago

stevenvachon commented 7 years ago
const FakeError = function() {
  this[Symbol.toStringTag] = 'Error';
};

Object.prototype.toString.call(new FakeError());
//-> [object Error]
mk-pmb commented 5 years ago

Hi, sorry for picking this up so late! Seems my notifications had failed me back then. I haven't seen this used in the wild; can you establish it is a somewhat widespread technique?

stevenvachon commented 5 years ago

Probably not a widespread technique, but supporting it would be more complete.

mk-pmb commented 5 years ago

I'm not convinced (yet) it's worth the extra code to maintain. I'll keep this open for more discussion; also anyone please feel free to vote thumbs up/down on the opening post. A feature poll will probably always be biased towards people interested, but it might nonetheless help convince me.

mk-pmb commented 4 years ago

I think I totally misunderstood your request. I assumed you wanted the fake error to be reported as an error. Now by chance I found out, that is the current behavior. So did you mean you want FakeErrors to not be counted as errors? Would make sense as, at least in your very basic example above, they don't carry .name, .message, .stack etc. (comparison script)

mk-pmb commented 4 years ago

I've considered this a bit more, and I decided that assigning a toStringTag Symbol of Error is clear indication that whoever writes the assignment, wants the object to be treated as an error.

is-error v2.2.2 on Node.js v8.16.2, Ubuntu 14.04.6 LTS trusty, accepts this effort and judges the object is a legitimate error (true result). This is the intended behavior.