Skip to content

Commit

Permalink
Merge pull request #580 from pagopa/feat/OI-256-error-code-map
Browse files Browse the repository at this point in the history
feat: [OI-256] error code map
  • Loading branch information
sebbalex authored Jan 3, 2025
2 parents df58217 + bf0c8a0 commit b9d6950
Showing 1 changed file with 62 additions and 37 deletions.
99 changes: 62 additions & 37 deletions src/oneid/oneid-fe/src/hooks/useLoginError.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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, ErrorData> = {
[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;
}
};

Expand Down

0 comments on commit b9d6950

Please sign in to comment.