【问题标题】:How to handle exception twice如何处理两次异常
【发布时间】: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


    【解决方案1】:

    我遇到了同样的问题,但能够通过在构造函数中为第二个参数 (rethrowException) 传递 false 来使其正常工作。

    constructor() {
        super (new _ArrayLogger(), false);
    }
    

    参数的名称会暗示正确的值确实是true;对我来说似乎有点落后,但它有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多