implement interceptor

This commit is contained in:
2024-06-13 19:32:17 +07:00
parent 7a07bf5b07
commit 33007acc77
4 changed files with 43 additions and 3 deletions

View 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();
});
});

View 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);
})
);
}
}