cic-staff-client/src/app/_interceptors/error.interceptor.ts

32 lines
961 B
TypeScript

import {Injectable, isDevMode} from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor, HttpErrorResponse
} from '@angular/common/http';
import {Observable, throwError} from 'rxjs';
import {catchError} from 'rxjs/operators';
import {ErrorDialogService} from '@app/_services';
@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
constructor(private errorDialogService: ErrorDialogService) {}
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
return next.handle(request).pipe(catchError((err: HttpErrorResponse) => {
if (isDevMode()) {
this.errorDialogService.openDialog({
message: err.error.message || err.statusText || 'Unknown Error',
status: err.status || 0
});
}
if ([401, 403].indexOf(err.status) !== -1) {
location.reload(true);
}
return throwError(err);
}));
}
}