1
|
import { TranslateService } from '@ngx-translate/core';
|
2
|
import { RTAErrorMessage } from './../../enums/rta-error-message';
|
3
|
import { Injectable } from '@angular/core';
|
4
|
import { MessageService } from 'primeng/api';
|
5
|
import { HttpErrorResponse } from '@angular/common/http';
|
6
|
|
7
|
@Injectable({
|
8
|
providedIn: 'root',
|
9
|
})
|
10
|
export class ErrorHandlingService {
|
11
|
|
12
|
constructor(private messageService: MessageService, private translate: TranslateService) { }
|
13
|
|
14
|
showHttpResponseError(error: HttpErrorResponse): void {
|
15
|
this.messageService.add({
|
16
|
severity: 'error',
|
17
|
summary: this.translate.instant('RTA_ERRORS.TITLE_ERROR'),
|
18
|
detail: this.httpErrorResponseToMessage(error)
|
19
|
});
|
20
|
}
|
21
|
|
22
|
showErrorMessage(message: string): void {
|
23
|
this.messageService.add({
|
24
|
severity: 'error',
|
25
|
summary: this.translate.instant('RTA_ERRORS.TITLE_ERROR'),
|
26
|
detail: message
|
27
|
});
|
28
|
}
|
29
|
|
30
|
clearErrors(): void {
|
31
|
this.messageService.clear();
|
32
|
}
|
33
|
|
34
|
private httpErrorResponseToMessage(error: HttpErrorResponse): string {
|
35
|
if (error && error.status === 0) {
|
36
|
return this.translate.instant(`RTA_ERRORS.${RTAErrorMessage.TIMEOUT.toString()}`);
|
37
|
}
|
38
|
const errMsg = this.translate.instant(`RTA_ERRORS.${error.message}`);
|
39
|
return errMsg ?? this.translate.instant('RTA_ERRORS.UNKNOWN_ERROR');
|
40
|
}
|
41
|
}
|