src/app/core/helpers/error.interceptor.ts
constructor(authenticationService: ApiAuthService)
|
intercept |
intercept(request: HttpRequest
|
Returns:
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 { ApiAuthService } from '../services/auth/api.auth.service';
@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
constructor(private authenticationService: ApiAuthService) { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(catchError(err => {
if ([401, 403].indexOf(err.status) !== -1) {
this.authenticationService.logout();
}
const error = err.error.message || err.statusText;
return throwError(() => new Error(error))
}))
}
}