You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// file: my-error-handler.tsimport{ErrorHandler,Injector,Injectable}from'@angular/core';import{Response}from'@angular/http';import{Router}from'@angular/router';
@Injectable()exportclassMyErrorHandlerextendsErrorHandler{privaterouter: Router;constructor(privateinj: Injector){super(false);this.router=inj.get(Router);// You may need to do this, because at the time of creation the DI-System isn't up}handleError(error: any): void{if(errorinstanceofResponse){// You may need to differentiate between several error typesconsterr=errorasResponse;if(err.status===401){// Token invalid or expired, I'm using JWTlocalStorage.removeItem('id_token');this.router.navigate(['/login','offline']);console.error('Token invalid or expired. Logged out.');}}else{console.error('APPLICATION ERROR: \''+error.message+'\'');console.error(error);}}}
Register error handler
// file: app.module.ts
@NgModule({declarations: [
...
],imports: [
...
],providers: [
...
{provide: ErrorHandler,useClass: MyErrorHandler}// Override default error handler class, which is the base class of our 'MyErrorHandler'],bootstrap: [AppComponent]})exportclassAppModule{constructor(){}}