implement interceptor
This commit is contained in:
16
src/app/interceptors/http-error-interceptor.service.spec.ts
Normal file
16
src/app/interceptors/http-error-interceptor.service.spec.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { HttpErrorInterceptorService } from './http-error-interceptor.service';
|
||||
|
||||
describe('HttpErrorInterceptorService', () => {
|
||||
let service: HttpErrorInterceptorService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(HttpErrorInterceptorService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
||||
22
src/app/interceptors/http-error-interceptor.service.ts
Normal file
22
src/app/interceptors/http-error-interceptor.service.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { HttpErrorResponse, HttpEvent, HttpHandler, HttpRequest } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Router } from '@angular/router';
|
||||
import { Observable, catchError, throwError } from 'rxjs';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class HttpErrorInterceptorService {
|
||||
|
||||
constructor(private router: Router) { }
|
||||
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
||||
return next.handle(req).pipe(
|
||||
catchError((error: HttpErrorResponse) => {
|
||||
if (error.status === 403) {
|
||||
this.router.navigate(['/error/error403']);
|
||||
}
|
||||
return throwError(error);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user