Skip to content

Instantly share code, notes, and snippets.

@a-patel
Created November 16, 2018 18:06
Show Gist options
  • Select an option

  • Save a-patel/3d46c377d06e3ba4b6fcfb1e862cbaf9 to your computer and use it in GitHub Desktop.

Select an option

Save a-patel/3d46c377d06e3ba4b6fcfb1e862cbaf9 to your computer and use it in GitHub Desktop.
Error Handler in Angular
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