You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

38 lines
1.5 KiB

  1. import { Injectable } from '@angular/core';
  2. import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor, HttpResponse } from '@angular/common/http';
  3. import { Observable, throwError } from 'rxjs';
  4. import { catchError, tap } from 'rxjs/operators';
  5. import { AccountService, AlertService } from '@app/_services';
  6. @Injectable()
  7. export class ErrorInterceptor implements HttpInterceptor {
  8. constructor(
  9. private accountService: AccountService,
  10. private alertService: AlertService,
  11. ) {}
  12. intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
  13. return next.handle(request).pipe(
  14. tap(evt => {
  15. if (evt instanceof HttpResponse) {
  16. if ((request.method === 'POST' || request.method === 'PATCH') && evt.status === 200) {
  17. // Erfolgsmeldung für POST und PATCH
  18. this.alertService.success('Saved', {autoClose: true});
  19. }
  20. }
  21. }),
  22. catchError(err => {
  23. if ([401, 403].includes(err.status) && this.accountService.userValue) {
  24. // auto logout if 401 or 403 response returned from api
  25. this.accountService.logout();
  26. }
  27. console.log(err);
  28. this.alertService.error(err.error.detail);
  29. const error = err.error?.message || err.statusText;
  30. return throwError(() => error);
  31. })
  32. );
  33. }
  34. }