hapijs / boom

HTTP-friendly error objects
Other
2.94k stars 192 forks source link

Extending Boom and unexpected instanceof behavior #285

Closed wallacethefmh closed 3 years ago

wallacethefmh commented 3 years ago

Support plan

Context

How can we help?

https://github.com/hapijs/boom/blob/80528a674af77703cb8d0be15b50a655225d5cae/lib/index.js#L92

Doesn't this code break the expected behavior of class hierarchy/polymorphism?

const Boom = require('@hapi/boom').Boom; undefined class A extends Boom{}; undefined class B extends A{}; undefined const a = new A(); undefined a instanceof B; true

I see that removing that part of the Boom class doesn't fix the problem, since the Boom constructor actually returns a decorated Error and not an instance of itself. It's a bit strange.

It took me a while to figure out why instanceof for a subclass wasn't behaving like all other javascript classes, maybe it would just be worth adding something to the docs explaining the implications of extending Boom?

kanongil commented 3 years ago

Interesting. So the problem is that sub-classes inherit the Symbol.hasInstance method? I would not have guessed that.