Skip to content

Instantly share code, notes, and snippets.

@dancornilov
Last active December 27, 2018 16:36
Show Gist options
  • Save dancornilov/1cbbd5ff365afa73f60fefce12d2d1a5 to your computer and use it in GitHub Desktop.
Save dancornilov/1cbbd5ff365afa73f60fefce12d2d1a5 to your computer and use it in GitHub Desktop.
Status HttpInterceptor
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