Last active
December 27, 2018 16:36
-
-
Save dancornilov/1cbbd5ff365afa73f60fefce12d2d1a5 to your computer and use it in GitHub Desktop.
Status HttpInterceptor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'rxjs/add/operator/do'; | |
import {HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse} from '@angular/common/http'; | |
import {Injectable} from '@angular/core'; | |
import {Observable} from 'rxjs/Observable'; | |
import {Router} from '@angular/router'; | |
@Injectable() | |
export class StatusInterceptor implements HttpInterceptor { | |
constructor(private router: Router) { | |
} | |
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
return next.handle(request).do((event: HttpEvent<any>) => { | |
if (event instanceof HttpResponse) { | |
// Show success message | |
} | |
}, (err: any) => { | |
if (err instanceof HttpErrorResponse) { | |
this.loader.hideLoader(); | |
switch(err.status){ | |
case 401: | |
this.auth.logout(); | |
this.router.navigate(['/']); | |
break; | |
case 403: | |
this.router.navigate(['/become-member']); | |
case 404: | |
this.router.navigate(['/not-found']); | |
break; | |
default: | |
break; | |
} | |
// Show error message | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment