From f1068847a5a022547d741e616d801b6c4899a7f6 Mon Sep 17 00:00:00 2001 From: FlayInAHook Date: Thu, 3 Dec 2020 00:43:28 +0100 Subject: [PATCH] added error interceptor --- src/app/app.module.ts | 2 ++ src/app/helper/http-error.interceptor.ts | 45 ++++++++++++++++++++++++ src/app/helper/token.interceptor.ts | 2 +- src/app/pages/login/login.component.html | 2 +- src/app/services/snackbar.service.ts | 18 +++++++--- 5 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 src/app/helper/http-error.interceptor.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 9505330..7462c86 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -47,6 +47,7 @@ import { MenuListItemComponent } from './components/menu-list-item/menu-list-ite import { SidenavProfileComponent } from './components/sidenav-profile/sidenav-profile.component'; import { NavService } from './components/menu-list-item/nav.service'; import { TokenInterceptor } from './helper/token.interceptor'; +import { HttpErrorInterceptor } from './helper/http-error.interceptor'; import { BikeComponent } from './pages/dataPages/bike/bike.component'; import { TableComponent, @@ -138,6 +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 new file mode 100644 index 0000000..a8627b0 --- /dev/null +++ b/src/app/helper/http-error.interceptor.ts @@ -0,0 +1,45 @@ +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 f6bbe2e..c44d50d 100644 --- a/src/app/helper/token.interceptor.ts +++ b/src/app/helper/token.interceptor.ts @@ -22,7 +22,7 @@ export class TokenInterceptor implements HttpInterceptor { if (error instanceof HttpErrorResponse && error.status === 401) { return this.handle401Error(request, next); } else { - return throwError(error); + //return throwError(error); } })); } diff --git a/src/app/pages/login/login.component.html b/src/app/pages/login/login.component.html index 29f2c04..119f960 100644 --- a/src/app/pages/login/login.component.html +++ b/src/app/pages/login/login.component.html @@ -12,7 +12,7 @@ - Passwort eingeben + Passwort eingebene