I have an application with an globalError handler like this:
import { Injectable, ErrorHandler, Injector } from "@angular/core";
import { Router } from "@angular/router";
@Injectable()
export class GlobalErrorHandler implements ErrorHandler {
constructor(
private injector: Injector) { }
public handleError(error: any) {
console.error("Something went wrong");
//.. Handle error here
}
}
This always worked in every constallation. If an error was thrown the global handler caught it and handled it.
Now after upgrading to RxJs 6.2.2 I understood that catching http errors changed.
Code errors still keep working but errors thrown by the HttpClient are not globally caught. The GlobalErrorHandler is not fired any more.
I am aware that I can handle errors within my service and that works fine like this:
doSomething() {
return this.http
.get("http://someURL").pipe(
map((res: any) => { console.log(res) }),
catchError(this.handleError<any>(`blabla`))
);
}
/**
* Handle Http operation that failed.
* Let the app continue.
* @param operation - name of the operation that failed
* @param result - optional value to return as the observable result
*/
private handleError<T>(operation = 'operation', result?: T) {
return (error: any): Observable<T> => {
console.log("Now throwing error");
// TODO: send the error to remote logging infrastructure
console.error(error); // log to console instead
// Let the app keep running by returning an empty result.
// ToDo: Global Error handler hier aufrufen....
return of(result as T);
};
}
But I actually would like to handle all errors centrally.
What is the correct way with Angular 6 and RxJs 6 to handle errors centrally?