角度4,自定义ErrorHandler不能识别自定义错误 [英] Angular 4, custom ErrorHandler doesn't recognize custom Error
问题描述
我尝试创建一个自定义全局 ErrorHandler ,并遵循详细的说明这里
应用程序错误处理程序(只是重要的部分)
@Injectable()
export class ApplicationErrorHandler extends ErrorHandler {
构造函数(私人注入器:注入器){
super(false);
}
handleError(error:any):void {
if(Error instanceof ApplicationError || error.originalError instanceof ApplicationError){
this。 addError(error.originalError);
}
else {
super.handleError(error);
}
}
应用模块 / p>
providers:[
{
提供:ErrorHandler,
useClass:ApplicationErrorHandler
}
],
app.component (只有重要的部分)
public ngOnInit():void {
const error = new ApplicationError();
error.message =fehler;
抛出错误;
}
应用程序错误
export class ApplicationError implements Error {
name:string;
message:string;
httpStatus ?: number = 404;
applicationStatus ?: number;
errorMessageTranslationkey:string;
处理:boolean = false;
}
在我的app.component中,我抛出一个ApplicationError(在ngOnInit中),我的ErrorHandler被成功调用
但是我的错误 handleError
始终是类型错误
和 error.originalError
永远是 undefined
无论我是否抛出我的自定义错误,而如果
永远不会解决为真。
我不知道为什么会发生这种情况。
我看到的是,错误得到,所以我假设,包装,因为当我调试我看到错误:错误:[object:Object] at viewWrappedDebugError(vendor.bundle.js)
任何想法可能会导致此问题以及如何解决?
EDIT
怀疑它与Debugmode有关。一旦我启用prodmode与 enableProdMode();
它的工作原理。
仍然这不是真的
如何处理角度调试模式下的自定义错误?
您遇到此问题,因为 ApplicationError
不是错误
。
您可以使用以下代码创建自定义错误:
export class ApplicationError extends Error {
httpStatus ?: number = 404;
applicationStatus ?: number;
errorMessageTranslationkey:string;
处理:boolean = false;
构造函数(message ?: string){
super(message);
this.name = ApplicationError.name;
Object.setPrototypeOf(this,ApplicationError.prototype);
}
}
与创建自定义错误的主题相关,检查还有这些链接,以便对此主题有一个完整的想法:
为什么这需要一个错误的实例?
因为您的错误通过以下方法: / p>
function viewWrappedDebugError(err,context){
if(!(err instanceof Error)){
/ /错误不是错误实例不一个栈,
//所以可以把它们包装成一个新的错误对象...
err = new Error(err.toString());
}
_addDebugContext(err,context);
return err;
}
可在 errors.ts
。 / p>
因此,如果您没有抛出错误
实例,则会创建一个新实例。
拦截ErrorHandler中未捕获的承诺
另一个错误的情况是当您返回拒绝的承诺时,您的角色生命周期调用的方法(例如:handler,lifecycle-hook)。
export class AppComponent实现OnInit {
ngOnInit(){
return new Promise((resolve,reject)=> reject(new ApplicationError()));
}
}
因此,错误处理代码可能如下所示:来自@ angular / core的p>
import {ErrorHandler,Injectable,Injector}
import {ApplicationError} from./ApplicationError;
@Injectable()
导出类ApplicationErrorHandler扩展ErrorHandler {
私有错误:ApplicationError [] = [];
构造函数(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);
}
}
I tried to create a custom global ErrorHandler and followed the instruction detailed here
application-error-handler (just the important parts)
@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);
}
}
app-module
providers: [
{
provide: ErrorHandler,
useClass: ApplicationErrorHandler
}
],
app.component (only the important part)
public ngOnInit(): void {
const error = new ApplicationError();
error.message = "fehler";
throw error;
}
application-error
export class ApplicationError implements Error {
name: string;
message: string;
httpStatus?: number = 404;
applicationStatus?: number;
errorMessageTranslationkey: string;
handled: boolean = false;
}
in my app.component I throw an ApplicationError (in ngOnInit), my ErrorHandler gets called successfully.
But my error in handleError
is always of type Error
and error.originalError
is always undefined
no matter if I throw my custom error and there the if
will never resolve to true.
I have no idea why and how this happens.
What I see is that the error gets, so I assume, wrapped because when I debug I see error: Error: [object: Object] at viewWrappedDebugError (vendor.bundle.js)
Any idea what might cause this issue and how I can resolve it?
EDIT
As suspected it has something to do with Debugmode. As soon as I enable prodmode with enableProdMode();
it works as expected.
Still this doesn't really answer my question yet.
How can I handle my custom error in angular's debug mode?
You encounter this problem because ApplicationError
is not an Error
.
You can use the following code in order to create a custom 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);
}
}
Related to the topic of creating a custom error, check also these links in order to have a full idea on the subject:
Why this needs to be an instance of Error? Because your error pass through the following method:
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;
}
Code available at errors.ts
.
Therefore if you are not throwing an Error
instance, then a new instance is created.
Intercepting uncatched promises in ErrorHandler
Another error case is when you return a promise which becomes rejected, from your method that is called by the Angular lifecycle (eg: handler, lifecycle-hook).
export class AppComponent implements OnInit {
ngOnInit() {
return new Promise((resolve, reject) => reject(new ApplicationError()));
}
}
Therefore the error handling code can look like:
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);
}
}
这篇关于角度4,自定义ErrorHandler不能识别自定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!