Advertisement
andreadc

Untitled

Mar 14th, 2020
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. // NavBar Template
  2. <div class="errors" *ngIf="errorInterceptor.errors.value | async">
  3. <div class="error"><b>prova {{errorInterceptor.errors.value}}</b></div>
  4. </div>
  5.  
  6. //ErrorInterceptor
  7. @Injectable()
  8. export class ErrorInterceptor implements HttpInterceptor {
  9. constructor(private authenticationService: AuthenticationService, private router: Router) {}
  10.  
  11. errors = new BehaviorSubject<string>(null);
  12.  
  13. intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
  14. //next.handle(request) trasforma un HttpRequest in uno stream di HttpEvents,
  15. //restituisce un Observable <HttpEvent> 'Union type for all possible events on the response stream.'
  16. return next.handle(request).pipe(catchError(err => {
  17. if (err.status >= 400 && err.status < 500) {
  18. // auto logout if 401 response returned from api
  19. //this.authenticationService.logout();
  20. //location.reload(true);
  21. //console.error("Error:="+err.error.msg);
  22. //this.router.navigate(['/login']);
  23. console.error('error-interceptor:error status=' + err.status);
  24. this.errors.next(err.error);
  25. } else {
  26.  
  27. console.error('error-interceptor:err=' + err.error + ' statusText=' + err.statusText + ' status:' + err.status);
  28. this.errors.next(err.error);
  29.  
  30. }
  31.  
  32. const error = err.error.msg || err.statusText;
  33. return throwError(error);
  34. }))
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement