I am currently working on implementing an HttpInterceptor in Angular to catch errors and display them in a modal. In addition to error code and message, I want to include the body of the response for a more detailed description of the error (such as in the case of a 500 internal server error). How can I go about achieving this in my Angular project? (I am using version 4.3.6.)
Despite looking into similar questions, solutions like accessing HttpErrorResponse._body have not been successful for me. Furthermore, when examining the error response in the console, I noticed that HttpErrorResponse.error is null.
Below is a snippet of how my interceptor is currently structured:
@Injectable()
export class HttpErrorInterceptor implements HttpInterceptor {
public constructor(private httpErrorService: HttpErrorService) { }
public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).do(event => {
}, (error: HttpErrorResponse) => {
console.log('HTTPERROR INTERCEPTOR');
console.log(error);
if (error.status >= 400) {
this.httpErrorService.onError(error);
}
});
}
}