error-handling - 如何两次处理异常

标签 error-handling typescript angular

我扩展了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。当发生第二个,第三个..时,不会调用 super 。如何使其工作,以便原始console.log将错误记录到控制台,并且还进行了其他错误处理?

最佳答案

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

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

参数的名称表明正确的值确实是true;对我来说似乎有点倒退,但确实有效。

关于error-handling - 如何两次处理异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37588550/

相关文章:

Java 条件返回术语

spring - 在一页Struts中显示数据表并提交表单

Java输出控制台错误消息到文件?

css - :hover and :focus inline style in React doesn't work

javascript - Angular 2 : How to get a reference to a ViewChild in a child component

Angular (2+) 组件提供与投影内容的绑定(bind)

swift - “ fatal error :在展开可选值时意外发现nil”是什么意思?

javascript - 如何从列表中过滤数据并以 Angular 从数据中删除现有房间

python - Dockerized Angular 4 和 Django 编译成功但是 localhost :4200 is not working

mysql - Angular2 使用 observables 解析 json 对象