0

I have this simple function which should return trueconsole debugger

I returns false though, but console debugger shows that this is the UIError object. Nevertheless:

exception.originalException instanceOf UIError

returns false ! I see in the debugger that it is indeed an UIError class. Why ?

UIError extends Error class

enter image description here

private isAnUIError(exception: any): boolean {
    return (exception.originalException) instanceof UIError || exception instanceof  UIError;
}

export class UIError extends Error {
constructor (private message: string) {
super();
this.message = message;
 }
adam nowak
  • 785
  • 2
  • 9
  • 17
  • 1
    What is `UIError`? – Oriol Jun 08 '16 at 19:06
  • 1
    Not sure, it could be that it is *the* `UIError` *class* itself? Try `exception.originalException == UIError`. Alternatively, does at least `exception.originalException instanceof Error` work? – Bergi Jun 08 '16 at 19:06
  • UIError extends Error – adam nowak Jun 08 '16 at 19:06
  • exception.originalException instanceOf Error returns true, but I don'w want that. I want only catch the UIError objects – adam nowak Jun 08 '16 at 19:07
  • 1
    @adamnowak Can you please post your code that allows us to reproduce this, instead of showing pretty debugger pictures? Notice that the "name" (or "class name") of an object in the debugger can be quite misleading, especially in multiframe environments. – Bergi Jun 08 '16 at 19:12
  • Are you using `instanceOf` or `instanceof`? – Mr. Meeseeks Jun 08 '16 at 19:13
  • How is `UIError` defined? If it is defined incorrectly it will be an `Error` with the name `UIErrror` which will display in the debugger as a `UIError` but cause `instanceof` to fail. The answer lies purely in how `UIError` is implemented. – Sukima Jun 08 '16 at 19:15
  • I added the implementation of UIError and the checko function – adam nowak Jun 08 '16 at 19:19
  • @adamnowak: And how do you call it? `isAnUIError(new UIError)`? Also, how are you transpiling and where are you executing this? Notice that many enviroments simply don't support subclassing builtins yet. – Bergi Jun 08 '16 at 19:44

1 Answers1

0

If typescript is anything like Babel ES6 Error can not be extended like a normal class could be. This is nasty wart in JavaScript. The work around is to directly manage the new error's prototype. I explained this in another SO Answer regarding ES6 and Babel transpiling.

Here is how I would expect this to look:

class UIError {
  constructor(message) {
    this.name = 'UIError';
    this.message = message;
    this.stack = new Error().stack; // Optional
  }
}
UIError.prototype = Object.create(Error.prototype);

Keep in mind that super() does nothing with Error because Error is not a typical constructor function (class). It means nothing to perform

Error.apply(this, [].slice.call(arguments, 0))

(which is essentially what a super() does) since the Error() function doesn't do anything with this but instead returns a new Error. This is a know caveat regarding Error in JavaScript.

Community
  • 1
  • 1
Sukima
  • 9,965
  • 3
  • 46
  • 60
  • You will need to distinguish between ES5 `Error` implementations and ES6 ones. – Bergi Jun 08 '16 at 19:45
  • I tried your approach but got: "TypeError: Cannot assign to read only property 'prototype' of function 'class MyError'" – rideronthestorm Dec 07 '17 at 17:13
  • @rinderonthestorm I think babel is in transition. To fix your error don’t use `class` and go back to `function`. I also think newer versions of babel will allow extending `Error` as expected. Not sure. – Sukima Dec 07 '17 at 18:50