import { Injectable } from '@angular/core' ; import { HttpRequest , HttpHandler , HttpEvent , HttpInterceptor , HttpErrorResponse } from '@angular/common/http' ; import { Observable , throwError } from 'rxjs' ; import { catchError } from 'rxjs/operators' ; import { AuthService } from '../../auth/services/auth.service' ; import { Router } from '@angular/router' ; @ Injectable ( ) export class HttpErrorInterceptor implements HttpInterceptor { public readonly CONNECTION_REFUSED : number = 0 ; public readonly BAD_REQUEST : number = 400 ; public readonly UNAUTHORIZED : number = 401 ; public readonly FORBIDDEN : number = 403 ; public readonly NOT_FOUND : number = 404 ; public readonly METHOD_NOT_ALLOWED : number = 405 ; public readonly NOT_ACCEPTABLE : number = 406 ; public readonly PAGE_GONE : number = 410 ; public readonly INTERNAL_SERVER_ERROR : number = 500 ; public readonly NOT_IMPLEMENTED : number = 501 ; private readonly INFO_ERRORS : number [ ] = [ this . BAD_REQUEST , this . NOT_FOUND , this . METHOD_NOT_ALLOWED , this . NOT_ACCEPTABLE , this . PAGE_GONE , this . NOT_IMPLEMENTED , this . FORBIDDEN ] ; private readonly REDIRECT_ERRORS : number [ ] = [ this . CONNECTION_REFUSED , this . UNAUTHORIZED , this . INTERNAL_SERVER_ERROR ] ; constructor ( private router , private authService ) { } public intercept ( request , next ) { return next . handle ( request ) . pipe ( catchError ( ( error ) => { const statusCode = error . status ; if ( this . INFO_ERRORS . indexOf ( statusCode ) !== - 1 ) { } else if ( this . REDIRECT_ERRORS . indexOf ( statusCode ) !== - 1 ) { if ( statusCode === this . CONNECTION_REFUSED || statusCode === this . INTERNAL_SERVER_ERROR ) { this . router . navigate ( [ '/connect' ] ) ; } else if ( statusCode === this . UNAUTHORIZED ) { this . authService . logout ( ) ; } else { this . router . navigate ( [ '/error/' , statusCode ] ) ; } } return throwError ( error ) ; } ) ) ; } }