Advertisement
xapu

Untitled

Dec 11th, 2017
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2.  
  3. import {
  4. HttpErrorResponse,
  5. HttpEvent,
  6. HttpHandler,
  7. HttpInterceptor,
  8. HttpRequest,
  9. HttpResponse
  10. } from '@angular/common/http';
  11.  
  12. import { Router } from '@angular/router';
  13.  
  14. import { Observable } from 'rxjs/Observable';
  15. import 'rxjs/add/operator/map';
  16. import 'rxjs/add/operator/catch';
  17. import 'rxjs/add/observable/throw';
  18.  
  19. @Injectable()
  20. export class ErrorInterceptor implements HttpInterceptor {
  21. constructor(private router: Router) { }
  22.  
  23. intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
  24. return next.handle(req).map((event: HttpEvent<any>) => {
  25. return event;
  26. }).catch((err: any, caught) => {
  27. if (err instanceof HttpErrorResponse) {
  28. this.router.navigate(['error', { stauts: err.status }]);
  29. return Observable.throw(err);
  30. }
  31. });
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement