Created
November 16, 2018 18:06
-
-
Save a-patel/3d46c377d06e3ba4b6fcfb1e862cbaf9 to your computer and use it in GitHub Desktop.
Error Handler in Angular
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { ErrorHandler, Injectable, Injector } from "@angular/core"; | |
| import { Router } from "@angular/router"; | |
| import { ModalService } from "../services/modal.service"; | |
| @Injectable() | |
| export class CustomErrorHandler implements ErrorHandler { | |
| constructor(private readonly injector: Injector) { } | |
| handleError(error: any) { | |
| const modalService = this.injector.get(ModalService); | |
| const router = this.injector.get(Router); | |
| if (!error.status) { return; } | |
| switch (error.status) { | |
| case 422: { | |
| modalService.alert(error.error); | |
| break; | |
| } | |
| case 401: { | |
| router.navigate(["/login"]); | |
| break; | |
| } | |
| default: { | |
| console.log(error); | |
| break; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment