0

I have this simple class:

export class MyError extends Error {}
export class AnotherError extends Error {}

export class CustomExceptionHandler implements ExceptionHandler {
    call(exception: any, stackTrace = null, reason = null) {
        if (exception instanceof MyError) {
            console.log("Here is MyError error");
        }
    }
}

I throw an error somewhere in the code:

throw new MyError()

Nevertheless the console.log never appears. The if statement is not satisfied. I want to be able to throw custom errors and handle them based on type. Why isn't this working? The output of console.log(exception) is:

ViewWrappedException {
    _wrapperMessage: "Error in ./CenterOnMeButton class CenterOnMeButton - inline template:0:28", 
    _originalException: MyError, 
    _originalStack: undefined, 
    _context: DebugContext, 
    _wrapperStack: "Error: Error in ./CenterOnMeButton class CenterOnM…tp://localhost:3000/polyfills.bundle.js:14981:35)"…
}

_context: DebugContext
_originalException: MyError
_originalStack: undefined
_wrapperMessage: "Error in ./CenterOnMeButton class CenterOnMeButton - inline template:0:28"
_wrapperStack: "Error: Error in ./CenterOnMeButton class CenterOnMeButton - inline template:0:28
    at ViewWrappedException.WrappedException [as constructor] (http://localhost:3000/vendor.bundle.js:2025:32)
    at new ViewWrappedException (http://localhost:3000/vendor.bundle.js:20847:17)
    at DebugAppView._rethrowWithContext (http://localhost:3000/vendor.bundle.js:42584:24)
    at http://localhost:3000/vendor.bundle.js:42597:24
    at http://localhost:3000/vendor.bundle.js:34994:37
    at http://localhost:3000/vendor.bundle.js:35057:94
    at ZoneDelegate.invoke (http://localhost:3000/polyfills.bundle.js:14881:30)
    at Object.onInvoke (http://localhost:3000/vendor.bundle.js:33274:42)
    at ZoneDelegate.invoke (http://localhost:3000/polyfills.bundle.js:14880:36)
    at Zone.runGuarded (http://localhost:3000/polyfills.bundle.js:14788:49)
    at NgZoneImpl.runInnerGuarded (http://localhost:3000/vendor.bundle.js:33307:79)
    at NgZone.runGuarded (http://localhost:3000/vendor.bundle.js:21420:74)
    at HTMLDivElement.outsideHandler (http://localhost:3000/vendor.bundle.js:35057:62)
    at ZoneDelegate.invokeTask (http://localhost:3000/polyfills.bundle.js:14914:39)
    at Zone.runTask (http://localhost:3000/polyfills.bundle.js:14814:49)
    at HTMLDivElement.ZoneTask.invoke (http://localhost:3000/polyfills.bundle.js:14981:35)"
context
Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
adam nowak
  • 785
  • 2
  • 9
  • 17

0 Answers0