Error Handling In Angular 7

Why Handle Errors

Handling errors is the most important part of application development. These errors hinder our work. The application can throw an error when something goes wrong. Angular handles the errors, but it does nothing except writing it in the console. Well, that is not useful either to the user or to the developer. Unexpected errors can happen any time, like a connection break, a null exception, no internet access, an unauthorized user, or a session expired. 

There are two types of error handling mechanism in Angular.

  • Client-side Errors
  • HTTP Errors.

HTTP Errors

The HTTP Errors are thrown when you send an HTTP Request using the HTTPClient Module. These errors again fall into two categories. One is generated by the server like an unauthorized user, session expired, Server down, etc. The other one is generated at the client-side while trying to generate the HTTP Request. These errors could be network error, error while generating the request, etc.

Client-side HTTP errors 

All errors that occur by a code are called client-side HTTP errors. It shows there is something wrong in the code.

How to handle such type of errors?  

There are two ways to handle these errors in Angular. 

  • Default Error Handling
  • Global Error Handling

Default Error Handling

In this approch, we can simply use the try-catch method for handling the error. It is a traditional way to handle these errors.
 
Syntax

  1. handelerror()  
  2.  {  
  3.    try  
  4.    {  
  5.     // Your code here  
  6.    }  
  7.    catch(error)  
  8.    {  
  9.      // handel error here  
  10.    }  
  11.  }  

Global Error Handling

 
Global Error Handling in Angular is handled by the Errorhandler class which is part of the @angular/core module. This is a global error handler class which catches all exceptions thrown by the application. In this approach, we create a class that will be implemented by the ErrorHandler class.

Syntax
  1. import { ErrorHandler, Injectable } from '@angular/core';  
  2.   
  3. @Injectable()  
  4. export class Myerrorhandelor implements ErrorHandler {  
  5.     constructor() {   
  6.     }  
  7.     handleError(error) {  
  8.        console.error('An error occurred:', error.message);  
  9.        console.error(error);  
  10.        alert(error);  
  11.    }  
  12.   }  

App.Module.ts

  1. import { BrowserModule } from '@angular/platform-browser';  
  2. import { NgModule, ErrorHandler } from '@angular/core';  
  3.   
  4. import { AppRoutingModule } from './app-routing.module';  
  5. import { AppComponent } from './app.component';  
  6. import { Myerrorhandelor } from "./myerrorhandelor";  
  7. @NgModule({  
  8.   declarations: [  
  9.     AppComponent  
  10.   ],  
  11.   imports: [  
  12.     BrowserModule,  
  13.     AppRoutingModule  
  14.   ],  
  15.   providers: [{provide: ErrorHandler, useClass: Myerrorhandelor}],  
  16.   bootstrap: [AppComponent]  
  17. })  
  18. export class AppModule { }  

Summary

 
I hope you have learned from this article. It is a very useful functionality and it will help you.