Angular 4,自定义ErrorHandler无法识别自定义错误

Ari*_*ael 8 error-handling typescript angular

我尝试创建一个自定义的全局ErrorHandler,并按照此处详述的说明进行操作

应用程序错误处理程序(只是重要部分)

@Injectable()
export class ApplicationErrorHandler extends ErrorHandler {

    constructor(private injector: Injector) {
        super(false);
    }

    handleError(error: any): void {

        if (error instanceof ApplicationError || error.originalError instanceof ApplicationError) {
            this.addError(error.originalError);
        }
        else {
            super.handleError(error);
        }
    }
Run Code Online (Sandbox Code Playgroud)

应用模块

 providers: [
    {
        provide: ErrorHandler,
        useClass: ApplicationErrorHandler
    }
],
Run Code Online (Sandbox Code Playgroud)

app.component(只有重要部分)

public ngOnInit(): void { 
    const error = new ApplicationError();
    error.message = "fehler";
    throw error;
} 
Run Code Online (Sandbox Code Playgroud)

应用程序错误

export class ApplicationError implements Error {
    name: string;
    message: string;
    httpStatus?: number = 404;
    applicationStatus?: number;
    errorMessageTranslationkey: string;
    handled: boolean = false;
}
Run Code Online (Sandbox Code Playgroud)

在我的app.component中我抛出一个ApplicationError(在ngOnInit中),我的ErrorHandler被成功调用.但我的错误handleError总是类型的Errorerror.originalError始终是undefined没有问题,如果我把我的自定义错误,并在那里if永远不会解决为true.

我不知道为什么以及如何发生这种情况.我所看到的是错误得到,所以我认为,包装因为我调试时看到了error: Error: [object: Object] at viewWrappedDebugError (vendor.bundle.js)

知道什么可能导致这个问题以及我如何解决它?

编辑 怀疑它与Debugmode有关.一旦我启用prodmode enableProdMode();它就按预期工作.

这仍然没有真正回答我的问题.

如何在angular的调试模式下处理自定义错误?

and*_*eim 20

你遇到这个问题因为ApplicationError不是Error.

您可以使用以下代码来创建自定义错误:

export class ApplicationError extends Error {

  httpStatus?: number = 404;
  applicationStatus?: number;
  errorMessageTranslationkey: string;
  handled: boolean = false;

  constructor(message?: string) {
    super(message);
    this.name = ApplicationError.name;
    Object.setPrototypeOf(this, ApplicationError.prototype);
  }
}
Run Code Online (Sandbox Code Playgroud)

与创建自定义错误的主题相关,还要检查这些链接,以便对主题有完整的了解:

为什么这需要成为错误的实例? 因为您的错误通过以下方法:

function viewWrappedDebugError(err, context) {
    if (!(err instanceof Error)) {
        // errors that are not Error instances don't have a stack,
        // so it is ok to wrap them into a new Error object...
        err = new Error(err.toString());
    }
    _addDebugContext(err, context);
    return err;
}
Run Code Online (Sandbox Code Playgroud)

代码可在errors.ts.

因此,如果您没有抛出Error实例,则会创建一个新实例.

在ErrorHandler中拦截未捕获的promise

另一个错误情况是,从Angular生命周期调用的方法返回一个被拒绝的promise(例如:handler,lifecycle-hook).

export class AppComponent implements OnInit {

  ngOnInit() {
    return new Promise((resolve, reject) => reject(new ApplicationError()));
  }
}
Run Code Online (Sandbox Code Playgroud)

因此,错误处理代码可能如下所示:

import {ErrorHandler, Injectable, Injector} from "@angular/core";
import {ApplicationError} from "./ApplicationError";

@Injectable()
export class ApplicationErrorHandler extends ErrorHandler {

  private errors: ApplicationError[] = [];

  constructor(private injector: Injector) {
    super(false);
  }

  handleError(error: any): void {

    if (error instanceof ApplicationError) {
      this.addError(error);
    }
    else {
      if(error.rejection instanceof ApplicationError) {
        this.addError(error.rejection);
      }
      else {
        super.handleError(error);
      }
    }
  }

  addError(error: ApplicationError) {
    this.errors.push(error);
  }
}
Run Code Online (Sandbox Code Playgroud)