Error handling is one of the most cruicial but neglected item of frontend development. There are number of errors that can happen on frontend and they are of different types. Following are different types of errors that our users can encounter
- Javascript errors e.g when you divide a number by a dynamic input and the input is 0
- Errors in calling server APIs.
I want to discuss the server errors in this article. When we call an API there can be several problems, for example on a login authentication form when you call your server API to authenticate user, you can have following possibilities
- The device has lost internet connection
- The code on server is not working properly
- You have made a typo in the API end point name
- The user account is not valid and server returns an error code or an error status
All these scenarios must be handled and gracefully inform the user about the error with a suggestion to recover from the error state. Obviously you cannot write code in all API calls to handle all error conditions. Surely you don't have to do this. In Angular you can write HTTPInterceptor to handle these conditions at one location in your code and for special cases where a component wants to handle on it own you can pass the error on and component and provide its own implementation too.
Following are the steps to implement the error handler.
Add error handling logic your interceptor.
Step 3:
Write MessageService to display an elegant error message in front of user. For simplicity I am passing the actual API error message here to the message service, you can refine this implementation and provide better error messages based on the error status.
Step 4:
Make your angular engine aware about your newly created interceptor. Import it in your module file and add it in your providers list like the following
Following are the steps to implement the error handler.
Step 1:
Write a class that extends HttpInterceptor interface from Angular common http library.
import { Injectable } from '@angular/core'; import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http'; import { Observable } from 'rxjs'; @Injectable() export class MyInterceptor implements HttpInterceptor { intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { return next.handle(req); } }Step 2:
Add error handling logic your interceptor.
In the above code snippet the MessageService was injected in the interceptor using the constructor as we do in all other Angular components.intercept ( request: HttpRequest<any>, next: HttpHandler ): Observable<HttpEvent<any>> { return next.handle(request) .pipe( retry(1), catchError( (error: HttpErrorResponse) => { if (error.error instanceof ErrorEvent) { console.log('Client side error'); } else { this.messageService.add(error.message); } return throwError(error); }) ); }
Step 3:
Write MessageService to display an elegant error message in front of user. For simplicity I am passing the actual API error message here to the message service, you can refine this implementation and provide better error messages based on the error status.
Step 4:
Make your angular engine aware about your newly created interceptor. Import it in your module file and add it in your providers list like the following
That's all you have to do to handle errors in Angular2 application.import { HTTP_INTERCEPTORS } from '@angular/common/http'; import { MyInterceptor } from './my-interceptor';
...
providers: [ { provide: HTTP_INTERCEPTORS, useClass: MyInterceptor, multi: true }, ]...
Comments
Post a Comment