From bf0c8a0a84165eb0d8581f05dc06b9e7166245c6 Mon Sep 17 00:00:00 2001 From: Michele Moio Date: Thu, 19 Dec 2024 08:08:06 +0100 Subject: [PATCH] feat: [OI-256] error code map --- .../oneid-fe/src/hooks/useLoginError.tsx | 99 ++++++++++++------- 1 file changed, 62 insertions(+), 37 deletions(-) diff --git a/src/oneid/oneid-fe/src/hooks/useLoginError.tsx b/src/oneid/oneid-fe/src/hooks/useLoginError.tsx index 3b758dfd..602250fe 100644 --- a/src/oneid/oneid-fe/src/hooks/useLoginError.tsx +++ b/src/oneid/oneid-fe/src/hooks/useLoginError.tsx @@ -5,44 +5,69 @@ export type ErrorData = { description: string; }; +export enum ERROR_CODE { + TOO_MANY_ATTEMPTS = '19', + INCOMPATIBLE_CREDENTIALS = '20', + AUTH_TIMEOUT = '21', + DENIED_BY_USER = '22', + SUSPENDED_OR_REVOKED = '23', + CANCELED_BY_USER = '25', + ID_NOT_SUPPORTED = '30', + MISSING_RESPONSE_TYPE = 'AUTHORIZATION_ERROR_RESPONSE_TYPE', + MISSING_CLIENT_ID = 'GENERIC_HTML_ERROR', + MISSING_IDP = 'AUTHORIZATION_ERROR_IDP', + IDP_ERROR = 'IDP_ERROR', + OI_ERROR = 'OI_ERROR', + SESSION_ERROR = 'SESSION_ERROR', + MISSING_REDIRECT_URI = 'CALLBACK_URI_NOT_FOUND', +} + +export const GENERIC_ERROR_DATA = { + title: t('loginError.generic.title'), + description: t('loginError.generic.description'), +}; + +export const erroMap: Record = { + [ERROR_CODE.TOO_MANY_ATTEMPTS]: { + title: t('loginError.tooManyAttempts.title'), + description: t('loginError.tooManyAttempts.description'), + }, + [ERROR_CODE.INCOMPATIBLE_CREDENTIALS]: { + title: t('loginError.incompatibleCredentials.title'), + description: t('loginError.incompatibleCredentials.description'), + }, + [ERROR_CODE.AUTH_TIMEOUT]: { + title: t('loginError.authTimeout.title'), + description: t('loginError.authTimeout.description'), + }, + [ERROR_CODE.DENIED_BY_USER]: { + title: t('loginError.deniedByUser.title'), + description: t('loginError.deniedByUser.description'), + }, + [ERROR_CODE.SUSPENDED_OR_REVOKED]: { + title: t('loginError.suspendedOrRevoked.title'), + description: t('loginError.suspendedOrRevoked.description'), + }, + [ERROR_CODE.CANCELED_BY_USER]: { + title: t('loginError.canceledbyUser.title'), + description: t('loginError.canceledbyUser.description'), + }, + [ERROR_CODE.ID_NOT_SUPPORTED]: GENERIC_ERROR_DATA, + [ERROR_CODE.MISSING_RESPONSE_TYPE]: GENERIC_ERROR_DATA, + [ERROR_CODE.MISSING_CLIENT_ID]: GENERIC_ERROR_DATA, + [ERROR_CODE.MISSING_IDP]: GENERIC_ERROR_DATA, + [ERROR_CODE.IDP_ERROR]: GENERIC_ERROR_DATA, + [ERROR_CODE.OI_ERROR]: GENERIC_ERROR_DATA, + [ERROR_CODE.SESSION_ERROR]: GENERIC_ERROR_DATA, + [ERROR_CODE.MISSING_REDIRECT_URI]: GENERIC_ERROR_DATA, +}; + export const useLoginError = () => { - const handleErrorCode = (errorCode: string): ErrorData => { - switch (errorCode) { - case '19': - return { - title: t('loginError.tooManyAttempts.title'), - description: t('loginError.tooManyAttempts.description'), - }; - case '20': - return { - title: t('loginError.incompatibleCredentials.title'), - description: t('loginError.incompatibleCredentials.description'), - }; - case '21': - return { - title: t('loginError.authTimeout.title'), - description: t('loginError.authTimeout.description'), - }; - case '22': - return { - title: t('loginError.deniedByUser.title'), - description: t('loginError.deniedByUser.description'), - }; - case '23': - return { - title: t('loginError.suspendedOrRevoked.title'), - description: t('loginError.suspendedOrRevoked.description'), - }; - case '25': - return { - title: t('loginError.canceledbyUser.title'), - description: t('loginError.canceledbyUser.description'), - }; - default: - return { - title: t('loginError.generic.title'), - description: t('loginError.generic.description'), - }; + const handleErrorCode = (errorCode: ERROR_CODE): ErrorData => { + if (errorCode in erroMap) { + return erroMap[errorCode]; + } else { + return GENERIC_ERROR_DATA; } };