Utilisation du système centralisé d'erreur dans toute l'application

This commit is contained in:
FyloZ 2021-02-17 00:13:34 -05:00
parent 1c5f244811
commit 98e5d7f36c
2 changed files with 10 additions and 37 deletions

View File

@ -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<any>(`${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)
})
}
}

View File

@ -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<boolean>()
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$
}