File

src/app/helpers/error.interceptor.ts

Index

Methods

Constructor

constructor(authenticationService: AuthenticationService, router: Router)
Parameters :
Name Type Optional
authenticationService AuthenticationService No
router Router No

Methods

intercept
intercept(request: HttpRequest, next: HttpHandler)
Parameters :
Name Type Optional
request HttpRequest<any> No
next HttpHandler No
Returns : Observable<HttpEvent<any>>
import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';

import { AuthenticationService } from '../services/authentication.service';
import { Router } from '@angular/router';

@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
    constructor(private authenticationService: AuthenticationService, private router: Router) {}

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        return next.handle(request).pipe(catchError(err => {
            if (err.status === 401) {
                // auto logout if 401 response returned from api
                this.authenticationService.logout();
                // tslint:disable-next-line: deprecation
                location.reload(true);
            }
            if (err.status === 500) {
                this.router.navigateByUrl('/500');
            }

            const error = err.error.message || err.statusText;
            return throwError(error);
        }));
    }
}

results matching ""

    No results matching ""