From 98e5d7f36c19bd3c35f8489ba34a1b11dbfffdc1 Mon Sep 17 00:00:00 2001 From: FyloZ Date: Wed, 17 Feb 2021 00:13:34 -0500 Subject: [PATCH] =?UTF-8?q?Utilisation=20du=20syst=C3=A8me=20centralis?= =?UTF-8?q?=C3=A9=20d'erreur=20dans=20toute=20l'application?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../accounts/services/account.service.ts | 36 +++++-------------- src/app/modules/shared/service/api.service.ts | 11 ++---- 2 files changed, 10 insertions(+), 37 deletions(-) diff --git a/src/app/modules/accounts/services/account.service.ts b/src/app/modules/accounts/services/account.service.ts index c59dd0d..572d2ce 100644 --- a/src/app/modules/accounts/services/account.service.ts +++ b/src/app/modules/accounts/services/account.service.ts @@ -6,6 +6,7 @@ import {HttpClient, HttpResponse} from '@angular/common/http' import {environment} from '../../../../environments/environment' import {ApiService} from '../../shared/service/api.service' import {Employee, EmployeePermission} from '../../shared/model/employee' +import {ErrorService} from '../../shared/service/error.service' @Injectable({ providedIn: 'root' @@ -16,7 +17,8 @@ export class AccountService implements OnDestroy { constructor( private http: HttpClient, private api: ApiService, - private appState: AppState + private appState: AppState, + private errorService: ErrorService ) { } @@ -38,24 +40,12 @@ export class AccountService implements OnDestroy { takeUntil(this.destroy$), ).subscribe({ next: employee => this.appState.authenticatedEmployee = employee, - error: err => { - if (err.status === 0 && err.statusText === 'Unknown Error' || err.status === 502) { - this.appState.isServerOnline = false - } else { - this.appState.isServerOnline = true - if (err.status === 404 || err.status === 403) { - console.error('No default user is defined on this computer') - } else { - console.error('An error occurred while authenticating the default user') - console.error(err) - } - } - } + error: err => this.errorService.handleError(err) }) } } - login(id: number, password: string, success: () => void, error: (err) => void) { + login(id: number, password: string, success: () => void) { const loginForm = {id, password} this.http.post(`${environment.apiUrl}/login`, loginForm, { withCredentials: true, @@ -72,14 +62,7 @@ export class AccountService implements OnDestroy { this.setLoggedInEmployeeFromApi() success() }, - error: err => { - if (err.status === 0 && err.statusText === 'Unknown Error' || err.status === 502) { - this.appState.isServerOnline = false - } else { - this.appState.isServerOnline = true - error(err) - } - } + error: err => this.errorService.handleError(err) }) } @@ -96,7 +79,7 @@ export class AccountService implements OnDestroy { this.checkAuthenticationStatus() success() }, - error: err => console.error(err) + error: err => this.errorService.handleError(err) }) } @@ -112,10 +95,7 @@ export class AccountService implements OnDestroy { ) .subscribe({ next: employee => this.appState.authenticatedEmployee = employee, - error: err => { - console.error('Could not get the logged in employee from the API: ') - console.error(err) - } + error: err => this.errorService.handleError(err) }) } } diff --git a/src/app/modules/shared/service/api.service.ts b/src/app/modules/shared/service/api.service.ts index 0ae027c..de167a2 100644 --- a/src/app/modules/shared/service/api.service.ts +++ b/src/app/modules/shared/service/api.service.ts @@ -6,6 +6,7 @@ import {AppState} from '../app-state' import {Router} from '@angular/router' import {map, share, takeUntil} from 'rxjs/operators' import {valueOr} from '../utils/optional.utils' +import {ErrorService} from './error.service' @Injectable({ providedIn: 'root' @@ -14,6 +15,7 @@ export class ApiService implements OnDestroy { private _destroy$ = new Subject() constructor( + private errorService: ErrorService, private http: HttpClient, private appState: AppState, private router: Router @@ -85,15 +87,6 @@ export class ApiService implements OnDestroy { : requestFn(httpOptions) .pipe(takeUntil(this._destroy$), map(r => r.body), share()) - const errorCheckSubscription = result$.subscribe({ - next: () => this.appState.isServerOnline = true, - error: err => { - console.error(err) - errorCheckSubscription.unsubscribe() - this.appState.isServerOnline = !(err.status === 0 && err.statusText === 'Unknown Error' || err.status === 502) - } - }) - return result$ }