perbaikan interceptor

This commit is contained in:
2024-06-26 14:26:15 +07:00
parent 4630bd2265
commit b5c35ab541
17 changed files with 326 additions and 28 deletions

View File

@@ -1,6 +1,7 @@
import { HttpErrorResponse, HttpEvent, HttpHandler, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { ToastrService } from 'ngx-toastr';
import { Observable, catchError, throwError } from 'rxjs';
@Injectable({
@@ -8,18 +9,34 @@ import { Observable, catchError, throwError } from 'rxjs';
})
export class HttpErrorInterceptorService {
constructor(private router: Router) { }
constructor(private router: Router, private toastr: ToastrService) { }
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']);
this.toastr.error("Error", error.message, {
timeOut: 3000,
closeButton: true,
});
// this.router.navigate(['/error/error403']);
} else if (error.status === 404) {
this.router.navigate(['/error/error404']);
this.toastr.error("Error", error.message, {
timeOut: 3000,
closeButton: true,
});
// this.router.navigate(['/error/error404']);
} else if (error.status === 500) {
this.router.navigate(['/error/error500']);
this.toastr.error("Error", error.message, {
timeOut: 3000,
closeButton: true,
});
// this.router.navigate(['/error/error500']);
} else if (error.status === 502) {
this.router.navigate(['/error/error500']);
this.toastr.error("Error", error.message, {
timeOut: 3000,
closeButton: true,
});
// this.router.navigate(['/error/error500']);
}
return throwError(error);
})