To capture a specific instance, follow these steps:
getData() {
this.http.get('http://localhost/php/data.json').subscribe((res) => {
this.data = res;
console.log(this.data);
}, (err:HttpErrorResponse) => {
consdole.log(err)
})
}
Using an interceptor
is recommended for centralized error
handling:
Create a file named http-interceptor.ts:
import { Injectable, Injector } from '@angular/core';
import {
HttpEvent,
HttpHeaders,
HttpInterceptor,
HttpResponse,
HttpErrorResponse,
HttpHandler,
HttpRequest
} from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
constructor(
private appService: AppService) {
}
/**
*
* @param req - parameter to handle http request
* @param next - parameter for http handler
*/
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const started = Date.now();
/**
* Handle newly created request with updated header (if given)
*/
return next.handle(req).do((event: HttpEvent<any>) => {
/**
* Sucessfull Http Response Time.
*/
if (event instanceof HttpResponse) {
const elapsed = Date.now() - started;
}
}, (err: any) => {
/**
* redirect to the error_handler route according to error status or error_code
* or show a modal
*/
if (err instanceof HttpErrorResponse) {
console.log(err);
}
});
}
}
In your module.ts file:
Add the following to providers:
{
provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor,
multi: true,
}
]