openaire-library/error-interceptor.service.ts

39 lines
1.2 KiB
TypeScript

import {Injectable} from "@angular/core";
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from "@angular/common/http";
import {Observable, throwError} from "rxjs";
import {catchError} from "rxjs/operators";
import {Session} from "./login/utils/helper.class";
import {Router} from "@angular/router";
import {LoginErrorCodes} from "./login/utils/guardHelper.class";
import {properties} from "../../environments/environment";
@Injectable()
export class ErrorInterceptorService implements HttpInterceptor {
constructor(private router: Router) {
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).pipe(catchError(err => {
if ((err.status === 0 && this.isRegistryService(req)) || err.status === 401 || err.status === 403) {
this.logOut();
}
return throwError(err);
}));
}
isRegistryService(req: HttpRequest<any>):boolean {
return req.url.indexOf(properties.registryUrl) !== -1;
}
logOut() {
Session.removeUser();
this.router.navigate(['/user-info'], {
queryParams: {
'errorCode': LoginErrorCodes.NOT_LOGIN,
'redirectUrl': this.router.url
}
});
}
}