added common error handling function

This commit is contained in:
Diamantis Tziotzios 2024-05-30 10:57:34 +03:00
parent b72adf92d5
commit 8f3dbe0ccd
1 changed files with 13 additions and 2 deletions

View File

@ -1,21 +1,32 @@
import { HttpErrorResponse } from '@angular/common/http'; import { HttpErrorResponse } from '@angular/common/http';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { SnackBarNotificationLevel, UiNotificationService } from '@app/core/services/notification/ui-notification-service';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
@Injectable() @Injectable()
export class HttpErrorHandlingService { export class HttpErrorHandlingService {
constructor( constructor(
protected language: TranslateService protected language: TranslateService,
protected uiNotificationService: UiNotificationService,
) { ) {
} }
handleBackedRequestError(errorResponse: HttpErrorResponse, messageOvverrides?: Map<number, string>) {
const error: HttpError = this.getError(errorResponse);
if (error.statusCode === 404) {
this.uiNotificationService.snackBarNotification(messageOvverrides?.has(error.statusCode) ? messageOvverrides?.get(error.statusCode) : 'NOT-FOUND', SnackBarNotificationLevel.Warning);
} else {
this.uiNotificationService.snackBarNotification(error.getMessagesString(), SnackBarNotificationLevel.Warning);
}
}
getError(errorResponse: HttpErrorResponse): HttpError { getError(errorResponse: HttpErrorResponse): HttpError {
const error: HttpError = new HttpError(); const error: HttpError = new HttpError();
error.statusCode = errorResponse.status; error.statusCode = errorResponse.status;
error.messages = this.parseMessages(error.statusCode, errorResponse); error.messages = this.parseMessages(error.statusCode, errorResponse);
try { try {
error.errorCode = + errorResponse.error.code; error.errorCode = + errorResponse.error.code;
} catch {} } catch { }
// if (error && error.error && error.error.error) { // if (error && error.error && error.error.error) {
// errorMsg = error.error.error; // errorMsg = error.error.error;