I want to catch HTTP errors in my global exception handler.
The exception handler is working for most exceptions, but observable exceptions are not caught. The exceptions that I want to catch are HTTP exceptions.
This is how I tried sending the HTTP observable error to the exception handler.
import { Injectable } from '@angular/core';
import { Request, XHRBackend, RequestOptions, Http, Response, RequestOptionsArgs } from '@angular/http';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import { HttpException } from '../exceptions/http-exception';
import {Observable} from 'rxjs/Observable';
@Injectable()
export class HttpErrorService extends Http {
constructor(backend: XHRBackend, defaultOptions: RequestOptions) {
super(backend, defaultOptions);
}
request(url: string | Request, options?: RequestOptionsArgs) {
return super.request(url, options).catch((error: Response) => {
// Bypass lint.
fail();
function fail() {
// Here I want to throw the exception to send it to the exception handler, but it should also execute return Observable.throw(error); So I can catch the exception at the subscribe.
throw new HttpException(error);
}
return Observable.throw(error);
});
}
}
This, of course, doesn't work because the code after the throw is not executed.
But the exception that is thrown is also not caught, probably because this is done in an observable.
Is there a way to catch the exception in the global exception handler, and the request is still available at subscribe((res) => {}, (errRes) => {/*here*/})?