【发布时间】:2016-06-02 09:58:11
【问题描述】:
我已经扩展了 ExceptionHandler 类:
import {Injectable, ExceptionHandler} from '@angular/core';
export class FirstError extends Error {
handle() {
console.log("This is firstError handler");
}
}
export class AnotherError extends Error {
handle() {
console.log("This is AnotherError handler");
}
}
export class _ArrayLogger {
res = [];
log(s: any): void { this.res.push(s); }
logError(s: any): void { this.res.push(s); }
logGroup(s: any): void { this.res.push(s); }
logGroupEnd() {};
}
@Injectable()
export class CustomExceptionHandler extends ExceptionHandler {
constructor() {
super (new _ArrayLogger(), true);
}
call(exception: any, stackTrace = null, reason = null) {
// I want to have original handler log exceptions to console
super.call(exception, stackTrace, reason);
// If exception is my type I want to do some additional actions
if (exception.originalException instanceof Error) {
exception.originalException.handle();
}
}
}
我的目标是让 super.call 将错误作为原始 Angular2 ExceptionHandler 记录到控制台,并且另外做我自己的异常处理。不幸的是,在这种情况下 super 只会在第一次抛出错误时被调用。当第二个,第三个 .. 发生时,超级不会被调用。如何使它工作,以便原始的 console.log 将错误记录到控制台,并且还完成了额外的错误处理?
【问题讨论】:
标签: error-handling typescript angular