Error Handling on a Global Scale
As delineated within the previous section, the GlobalErrorHandler category is registered as a provider within the ERP. once a fault happens at intervals the applying, this approach is known as. The error is provided as a parameter and should be out on a personal basis at intervals the technique. In our scenario, a dialogue looks at true where the error message has to be compelled to be displayed, and additionally the drawback is in addition recorded at intervals the browser console.
The dialogue window is going to be closed in spite of whether or not the error is thrown outside of ngZone as a result of it's opened during an asking from zone.run. This is often the case, as an example, if a lifecycle binding error like the ngOnInit perform happens during a part.
global-error-handler.ts
@Injectable()
export class GlobalErrorHandler implements ErrorHandler {
constructor(private errorDialogService: ErrorDialogService, private zone: NgZone) {}
handleError(error: Error) {
this.zone.run(() =>
this.errorDialogService.openDialog(
error.message || "Undefined client error"
));
console.error("Error from global error handler", error);
}
}
In our example, there is a method in the AppComponent called localError which throws an error:
localError() {
throw Error("The app component has thrown an error!");
}
So, if the primary button is pressed, this mechanism is named, and GlobalErrorHandler tackles the problem that was caused by displaying this dialogue:

Error when button is pressed
The user will then click the Close button to dismiss the error dialogue and return to the application. Of fact, the mechanism of an error may differ from case to case, necessitating additional measures. For example, it is frequently useful to jot down the error message to a move log go in the back finish, to browse the application to a different page, or to reset a certain state when observing functions. In this case, we usually only show the error dialogue.