From 759cc4b218591e91461a1ad9e9583ba994e828be Mon Sep 17 00:00:00 2001 From: sultanmyrza Date: Fri, 27 May 2022 00:38:01 +0800 Subject: [PATCH] fix(sign-up.page): convert generic backend error to more specific --- src/app/features/signup/signup.page.ts | 26 ++++++++++++++++++++++++-- src/app/shared/error/error.service.ts | 1 + src/assets/i18n/en-us.json | 4 +++- src/assets/i18n/zh-tw.json | 4 +++- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/app/features/signup/signup.page.ts b/src/app/features/signup/signup.page.ts index d7337ef68..27a0407e1 100644 --- a/src/app/features/signup/signup.page.ts +++ b/src/app/features/signup/signup.page.ts @@ -213,8 +213,30 @@ export class SignupPage { ) ), catchError((err: unknown) => { - // eslint-disable-next-line @typescript-eslint/no-magic-numbers - if (err instanceof HttpErrorResponse && err.status === 400) { + if ( + err instanceof HttpErrorResponse && + err.error.error?.type === 'duplicate_email' + ) { + return this.errorService.toastError$( + this.translocoService.translate( + 'error.diaBackend.duplicate_email' + ) + ); + } + if ( + err instanceof HttpErrorResponse && + err.error.error?.details?.username?.length > 0 + ) { + return this.errorService.toastError$( + this.translocoService.translate( + 'error.diaBackend.duplicate_username' + ) + ); + } + if ( + err instanceof HttpErrorResponse && + err.error.error?.type === 'invalid_referral_code' + ) { return this.errorService.toastError$( this.translocoService.translate( 'error.diaBackend.invalid_referral_code' diff --git a/src/app/shared/error/error.service.ts b/src/app/shared/error/error.service.ts index 46dbf5792..404cc2f86 100644 --- a/src/app/shared/error/error.service.ts +++ b/src/app/shared/error/error.service.ts @@ -30,6 +30,7 @@ export class ErrorService { timeoutError, unknownError, }) => { + // eslint-disable-next-line no-debugger if (typeof error === 'string') return error; if (error instanceof HttpErrorResponse) { if (error.status === HttpErrorCode.INVALID) diff --git a/src/assets/i18n/en-us.json b/src/assets/i18n/en-us.json index c46a93ae8..e0dcecafb 100644 --- a/src/assets/i18n/en-us.json +++ b/src/assets/i18n/en-us.json @@ -221,7 +221,9 @@ "unable_to_confirm_order": "Unable to confirm order. Please try again.", "unpaid_num_exceed_threshold": "Too much unpaid NUM. Please contact us with email to restore account functionality.", "invalid_network_app_name": "Invalid network app.", - "invalid_referral_code": "Invalide referral code" + "invalid_referral_code": "Invalid referral code", + "duplicate_email": "The email has already been registered", + "duplicate_username": "User with this username already exists" }, "wallets": { "emptyTransferAmount": "Please enter a valid transfer amount.", diff --git a/src/assets/i18n/zh-tw.json b/src/assets/i18n/zh-tw.json index dbe318404..0e2724df2 100644 --- a/src/assets/i18n/zh-tw.json +++ b/src/assets/i18n/zh-tw.json @@ -221,7 +221,9 @@ "unable_to_confirm_order": "無法確認訂單。請再試一次。", "unpaid_num_exceed_threshold": "太多未付的 NUM。請通過電子郵件與我們聯繫以恢復帳戶功能。", "invalid_network_app_name": "無效的網絡動作。", - "invalid_referral_code": "無效的推薦代碼" + "invalid_referral_code": "無效的推薦代碼", + "duplicate_email": "該電子郵件已註冊", + "duplicate_username": "用戶名已被其他使用者使用" }, "wallets": { "emptyTransferAmount": "請輸入有效轉帳金額。",