added error interceptor

pull/8/head
FlayInAHook 4 years ago
parent 5d10d57e50
commit f1068847a5

@ -47,6 +47,7 @@ import { MenuListItemComponent } from './components/menu-list-item/menu-list-ite
import { SidenavProfileComponent } from './components/sidenav-profile/sidenav-profile.component'; import { SidenavProfileComponent } from './components/sidenav-profile/sidenav-profile.component';
import { NavService } from './components/menu-list-item/nav.service'; import { NavService } from './components/menu-list-item/nav.service';
import { TokenInterceptor } from './helper/token.interceptor'; import { TokenInterceptor } from './helper/token.interceptor';
import { HttpErrorInterceptor } from './helper/http-error.interceptor';
import { BikeComponent } from './pages/dataPages/bike/bike.component'; import { BikeComponent } from './pages/dataPages/bike/bike.component';
import { import {
TableComponent, TableComponent,
@ -138,6 +139,7 @@ import { ProfileComponent } from './pages/profile/profile.component';
MatDatepickerModule, MatDatepickerModule,
DatePipe, DatePipe,
{ provide: HTTP_INTERCEPTORS, useClass: TokenInterceptor, multi: true }, { provide: HTTP_INTERCEPTORS, useClass: TokenInterceptor, multi: true },
{ provide: HTTP_INTERCEPTORS, useClass: HttpErrorInterceptor, multi: true },
{provide: MAT_DATE_LOCALE, useValue: 'de-DE'}, {provide: MAT_DATE_LOCALE, useValue: 'de-DE'},
], ],

@ -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<any>, next: HttpHandler): Observable<HttpEvent<any>> {
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);
})
)
}
}

@ -22,7 +22,7 @@ export class TokenInterceptor implements HttpInterceptor {
if (error instanceof HttpErrorResponse && error.status === 401) { if (error instanceof HttpErrorResponse && error.status === 401) {
return this.handle401Error(request, next); return this.handle401Error(request, next);
} else { } else {
return throwError(error); //return throwError(error);
} }
})); }));
} }

@ -12,7 +12,7 @@
</mat-form-field> </mat-form-field>
<mat-form-field (keyup.enter)="login()"> <mat-form-field (keyup.enter)="login()">
<mat-label>Passwort eingeben</mat-label> <mat-label>Passwort eingebene</mat-label>
<input <input
matInput matInput
[type]="hide ? 'password' : 'text'" [type]="hide ? 'password' : 'text'"

@ -9,10 +9,18 @@ export class SnackBarService {
constructor(private snackBar : MatSnackBar) { } constructor(private snackBar : MatSnackBar) { }
openSnackBar(message: string, action: string = "") { openSnackBar(message: string, action: string = "", error: boolean = false) {
if (error){
this.snackBar.open(message, action, {
duration: 5000,
panelClass: ['mat-toolbar', 'mat-warn', 'simple-snack-bar']
});
} else {
this.snackBar.open(message, action, { this.snackBar.open(message, action, {
duration: 5000, duration: 5000,
panelClass: ['mat-toolbar', 'mat-primary', 'simple-snack-bar'] panelClass: ['mat-toolbar', 'mat-primary', 'simple-snack-bar']
}); });
} }
}
} }

Loading…
Cancel
Save