I need to do global error handling in an angular 4 application. This is a mechanism ErrorHandlerthat works in some cases, but not for everyone. Example: when we receive a critical error, for example, a missing template or something else, ErrorHandlerignore it. When I use the wrong url for the template, I get the Zone.js error:
zone.js?fad3:567 Unhandled Promise rejection: Template parse errors:
'my-app' is not a known element:
Zone.js does not throw an exception, it is just a console error, so window.onerror does not work either.
Error Handler:
@Injectable()
export class CommonErrorHandler implements ErrorHandler {
constructor(private injector: Injector) {
}
public handleError(error: any): void {
console.log("error", error);
}
}
Register in app.module:
providers: [
AuthGuard,
{ provide: ErrorHandler, useClass: CommonErrorHandler }
}
UPD
Added plnkr example:
example
source
share