From dfd4a420c8911bfac6c2ffe94247dd2b378d5bc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adina=20=C8=9Aeudan?= Date: Tue, 19 Oct 2021 20:06:05 +0300 Subject: [PATCH] Improved error toaster --- src/lib/services/constants.ts | 1 + src/lib/services/error-message.service.ts | 2 +- src/lib/services/index.ts | 1 + src/lib/services/toaster.service.ts | 3 ++- 4 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 src/lib/services/constants.ts diff --git a/src/lib/services/constants.ts b/src/lib/services/constants.ts new file mode 100644 index 0000000..35fb16f --- /dev/null +++ b/src/lib/services/constants.ts @@ -0,0 +1 @@ +export const CONFLICT_ERROR_CODE = 409; diff --git a/src/lib/services/error-message.service.ts b/src/lib/services/error-message.service.ts index eabd69e..ab90712 100644 --- a/src/lib/services/error-message.service.ts +++ b/src/lib/services/error-message.service.ts @@ -10,7 +10,7 @@ export class ErrorMessageService { getMessage(error: HttpErrorResponse, defaultMessage?: string): string { return defaultMessage - ? (this._translateService.instant(defaultMessage) as string) + this._parseErrorResponse(error) + ? (this._translateService.instant(defaultMessage, { error: this._parseErrorResponse(error) }) as string) : this._parseErrorResponse(error); } diff --git a/src/lib/services/index.ts b/src/lib/services/index.ts index 214c413..8c903a1 100644 --- a/src/lib/services/index.ts +++ b/src/lib/services/index.ts @@ -3,3 +3,4 @@ export * from './toaster.service'; export * from './error-message.service'; export * from './generic.service'; export * from './composite-route.guard'; +export * from './constants'; diff --git a/src/lib/services/toaster.service.ts b/src/lib/services/toaster.service.ts index cce4ded..990947e 100644 --- a/src/lib/services/toaster.service.ts +++ b/src/lib/services/toaster.service.ts @@ -6,6 +6,7 @@ import { TranslateService } from '@ngx-translate/core'; import { HttpErrorResponse } from '@angular/common/http'; import { filter } from 'rxjs/operators'; import { ErrorMessageService } from './error-message.service'; +import { CONFLICT_ERROR_CODE } from './constants'; const enum NotificationType { SUCCESS = 'SUCCESS', @@ -52,7 +53,7 @@ export class Toaster { error(message: string, options?: Partial): ActiveToast { let resultedMsg; - if (options?.error) { + if (options?.error && options.error.status !== CONFLICT_ERROR_CODE) { resultedMsg = this._errorMessageService.getMessage(options.error, message); } else { resultedMsg = this._translateService.instant(message, options?.params) as string;