src/app/helpers/error.interceptor.ts
Methods |
constructor(authenticationService: AuthenticationService, router: Router)
|
|||||||||
|
Defined in src/app/helpers/error.interceptor.ts:10
|
|||||||||
|
Parameters :
|
| intercept | |||||||||
intercept(request: HttpRequest
|
|||||||||
|
Defined in src/app/helpers/error.interceptor.ts:13
|
|||||||||
|
Parameters :
Returns :
Observable<HttpEvent<any>>
|
import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { AuthenticationService } from '../services/authentication.service';
import { Router } from '@angular/router';
@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
constructor(private authenticationService: AuthenticationService, private router: Router) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(catchError(err => {
if (err.status === 401) {
// auto logout if 401 response returned from api
this.authenticationService.logout();
// tslint:disable-next-line: deprecation
location.reload(true);
}
if (err.status === 500) {
this.router.navigateByUrl('/500');
}
const error = err.error.message || err.statusText;
return throwError(error);
}));
}
}