From dc8d60748d2500225dc05cee07af6a53e6c5df7f Mon Sep 17 00:00:00 2001 From: FlayInAHook Date: Thu, 3 Dec 2020 00:57:50 +0100 Subject: [PATCH] merged interceptors --- src/app/app.module.ts | 2 +- src/app/helper/http-error.interceptor.ts | 45 ------------------------ src/app/helper/token.interceptor.ts | 25 +++++++++---- 3 files changed, 19 insertions(+), 53 deletions(-) delete mode 100644 src/app/helper/http-error.interceptor.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 7462c86..2d9f8e6 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -139,7 +139,7 @@ import { ProfileComponent } from './pages/profile/profile.component'; MatDatepickerModule, DatePipe, { provide: HTTP_INTERCEPTORS, useClass: TokenInterceptor, multi: true }, - { provide: HTTP_INTERCEPTORS, useClass: HttpErrorInterceptor, multi: true }, + {provide: MAT_DATE_LOCALE, useValue: 'de-DE'}, ], diff --git a/src/app/helper/http-error.interceptor.ts b/src/app/helper/http-error.interceptor.ts deleted file mode 100644 index a8627b0..0000000 --- a/src/app/helper/http-error.interceptor.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { - HttpEvent, - HttpInterceptor, - HttpHandler, - HttpRequest, - HttpResponse, - HttpErrorResponse - } from '@angular/common/http'; -import { Injectable } from '@angular/core'; - import { Observable, throwError } from 'rxjs'; - import { retry, catchError } from 'rxjs/operators'; - import {SnackBarService} from '../services/snackbar.service'; - - @Injectable() - export class HttpErrorInterceptor implements HttpInterceptor { - - constructor(private snackBar: SnackBarService){} - - intercept(request: HttpRequest, next: HttpHandler): Observable> { - return next.handle(request) - .pipe( - catchError((error: HttpErrorResponse) => { - - - let errorMessage = ''; - if (error.error instanceof ErrorEvent) { - - // client error - errorMessage = `Error: ${error.error.message}`; - } else { - // server error - if (error.status === 401){ - errorMessage = `Das eingegeben Passwort ist falsch`; - } else { - errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`; - } - - } - - this.snackBar.openSnackBar(errorMessage, "Ok", true); - return throwError(errorMessage); - }) - ) - } - } \ No newline at end of file diff --git a/src/app/helper/token.interceptor.ts b/src/app/helper/token.interceptor.ts index c44d50d..706d484 100644 --- a/src/app/helper/token.interceptor.ts +++ b/src/app/helper/token.interceptor.ts @@ -3,6 +3,7 @@ import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor, HttpErrorResponse import { AuthService } from '../services/auth.service'; import { Observable, throwError, BehaviorSubject } from 'rxjs'; import { catchError, filter, take, switchMap } from 'rxjs/operators'; +import { SnackBarService } from '../services/snackbar.service'; @Injectable() export class TokenInterceptor implements HttpInterceptor { @@ -10,7 +11,7 @@ export class TokenInterceptor implements HttpInterceptor { private isRefreshing = false; private requestTokenSubject: BehaviorSubject = new BehaviorSubject(null); - constructor(public authService: AuthService) { } + constructor(private authService: AuthService, private snackBar : SnackBarService) { } intercept(request: HttpRequest, next: HttpHandler): Observable> { @@ -18,12 +19,22 @@ export class TokenInterceptor implements HttpInterceptor { request = this.addToken(request, this.authService.getRequestToken()); } - return next.handle(request).pipe(catchError(error => { - if (error instanceof HttpErrorResponse && error.status === 401) { - return this.handle401Error(request, next); - } else { - //return throwError(error); - } + return next.handle(request).pipe(catchError((error: HttpErrorResponse) => { + let errorMessage = ''; + if (error.error instanceof ErrorEvent) { + //client error; + errorMessage = `Error: ${error.error.message}`; + } else { + //server error; + if (error.status === 401) { + + return this.handle401Error(request, next); + } else { + errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`; + } + } + this.snackBar.openSnackBar(errorMessage, "Ok", true); + //return throwError(errorMessage); })); }