扩展Angular ExceptionHandler,类型不比较

时间:2016-06-01 12:40:29

标签: exception-handling typescript angular custom-exceptions

我有这个简单的课程:

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");
        }
    }
}

我在代码中的某个地方抛出错误:

throw new MyError()

然而,console.log永远不会出现。 if语句不满足。我希望能够抛出自定义错误并根据类型处理它们。为什么这不起作用? console.log(exception)的输出是:

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

0 个答案:

没有答案