I created a custom typescript error, which based on several sources seems to go like this:
export class Exception extends Error {
constructor(public message: string) {
super(message);
this.name = 'Exception';
this.message = message;
this.stack = (<any>new Error()).stack;
}
toString() {
return this.name + ': ' + this.message;
}
}
export class SpecificException extends Exception {
}
In my code I then throw this using a simple:
throw new SpecificException('foo');
And elsewhere I catch it:
catch (e) {
var t1 = Object.getPrototypeOf(e);
var t2 = SpecificException.prototype;
if (e instanceof SpecificException) {
console.log("as expected");
}
else {
console.log("not as expected");
}
}
This code prints "not as expected". Any ideas why?
Later edit
As @basarat points out bellow, the error is of the expected type. Upon further investigation I realised that this was due to a module duplication having to do with my environment, possibly because of using mocha in watch mode.
e instanceof Exceptionande instanceof SpecificExceptionare true.