From 4ebe3c08f2512154465122ca020edf29b587fa4e Mon Sep 17 00:00:00 2001 From: kailash-b Date: Tue, 11 Feb 2025 15:48:00 +0530 Subject: [PATCH 1/4] Use BaseError instead of Error --- src/hooks/auth0-context.ts | 3 ++- src/hooks/reducer.ts | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/hooks/auth0-context.ts b/src/hooks/auth0-context.ts index e0e3baa5..91c02f28 100644 --- a/src/hooks/auth0-context.ts +++ b/src/hooks/auth0-context.ts @@ -19,6 +19,7 @@ import { RevokeOptions, ResetPasswordOptions, } from '../types'; +import BaseError from 'src/utils/baseError'; export interface Auth0ContextInterface extends AuthState { @@ -143,7 +144,7 @@ export interface AuthState { /** * An object representing the last exception */ - error: Error | null; + error: BaseError | null; /** * The user profile as decoded from the ID token after authentication */ diff --git a/src/hooks/reducer.ts b/src/hooks/reducer.ts index 94b520b6..3ea6625b 100644 --- a/src/hooks/reducer.ts +++ b/src/hooks/reducer.ts @@ -1,3 +1,4 @@ +import BaseError from 'src/utils/baseError'; import { User } from '../types'; import { deepEqual } from '../utils/deepEqual'; import { AuthState } from './auth0-context'; @@ -5,7 +6,7 @@ import { AuthState } from './auth0-context'; type Action = | { type: 'LOGIN_COMPLETE'; user: User } | { type: 'LOGOUT_COMPLETE' } - | { type: 'ERROR'; error: Error } + | { type: 'ERROR'; error: BaseError } | { type: 'INITIALIZED'; user: User | null } | { type: 'SET_USER'; user: User }; From cdfe8a241f215b11abdf41fb764e4118fbdfbf83 Mon Sep 17 00:00:00 2001 From: kailash-b Date: Thu, 13 Feb 2025 17:04:11 +0530 Subject: [PATCH 2/4] Bundle errors from android and iOS into platform agnostic error codes --- README.md | 25 ++++++++++ .../java/com/auth0/react/A0Auth0Module.java | 47 ++++++++++++++++- .../credentialsManagerError.ts | 50 +++++++++++++++++++ src/credentials-manager/index.ts | 19 ++++--- src/utils/baseError.ts | 6 ++- 5 files changed, 137 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 139aa95e..50fa6afc 100644 --- a/README.md +++ b/README.md @@ -493,6 +493,31 @@ try { } ``` +**Platform agnostic errors:** + +You can access the platform agnostic generic error codes as below : + +```js +try { + const credentials = await auth0.credentialsManager.getCredentials(); +} catch (error) { + console.log(e.generic_error_code); +} +``` + +| Generic Error Code | Corresponding Error Code in Android | Corresponding Error Code in iOS | +| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------- | +| `INVALID_CREDENTIALS` | `INVALID_CREDENTIALS` | | +| `NO_CREDENTIALS` | `NO_CREDENTIALS` | `noCredentials` | +| `NO_REFRESH_TOKEN` | `NO_REFRESH_TOKEN` | `noRefreshToken` | +| `RENEW_FAILED` | `RENEW_FAILED` | `renewFailed` | +| `STORE_FAILED` | `STORE_FAILED` | `storeFailed` | +| `REVOKE_FAILED` | `REVOKE_FAILED` | `revokeFailed` | +| `LARGE_MIN_TTL` | `LARGE_MIN_TTL` | `largeMinTTL` | +| `INCOMPATIBLE_DEVICE` | `INCOMPATIBLE_DEVICE` | | +| `CRYPTO_EXCEPTION` | `CRYPTO_EXCEPTION` | | +| `BIOMETRICS_FAILED` | OneOf
`BIOMETRIC_NO_ACTIVITY`,`BIOMETRIC_ERROR_STATUS_UNKNOWN`,`BIOMETRIC_ERROR_UNSUPPORTED`,
`BIOMETRIC_ERROR_HW_UNAVAILABLE`,`BIOMETRIC_ERROR_NONE_ENROLLED`,`BIOMETRIC_ERROR_NO_HARDWARE`,
`BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED`,`BIOMETRIC_AUTHENTICATION_CHECK_FAILED`,
`BIOMETRIC_ERROR_DEVICE_CREDENTIAL_NOT_AVAILABLE` | `biometricsFailed` | + ## Feedback ### Contributing diff --git a/android/src/main/java/com/auth0/react/A0Auth0Module.java b/android/src/main/java/com/auth0/react/A0Auth0Module.java index fcb4700a..66eec119 100644 --- a/android/src/main/java/com/auth0/react/A0Auth0Module.java +++ b/android/src/main/java/com/auth0/react/A0Auth0Module.java @@ -30,6 +30,43 @@ public class A0Auth0Module extends ReactContextBaseJavaModule implements ActivityEventListener { + public static final Map ERROR_CODE_MAP = new HashMap<>() {{ + put(CredentialsManagerException.Companion.getINVALID_CREDENTIALS(), "INVALID_CREDENTIALS"); + put(CredentialsManagerException.Companion.getNO_CREDENTIALS(), "NO_CREDENTIALS"); + put(CredentialsManagerException.Companion.getNO_REFRESH_TOKEN(), "NO_REFRESH_TOKEN"); + put(CredentialsManagerException.Companion.getRENEW_FAILED(), "RENEW_FAILED"); + put(CredentialsManagerException.Companion.getSTORE_FAILED(), "STORE_FAILED"); + put(CredentialsManagerException.Companion.getREVOKE_FAILED(), "REVOKE_FAILED"); + put(CredentialsManagerException.Companion.getLARGE_MIN_TTL(), "LARGE_MIN_TTL"); + put(CredentialsManagerException.Companion.getINCOMPATIBLE_DEVICE(), "INCOMPATIBLE_DEVICE"); + put(CredentialsManagerException.Companion.getCRYPTO_EXCEPTION(), "CRYPTO_EXCEPTION"); + put(CredentialsManagerException.Companion.getBIOMETRIC_ERROR_NO_ACTIVITY(), "BIOMETRIC_NO_ACTIVITY"); + put(CredentialsManagerException.Companion.getBIOMETRIC_ERROR_STATUS_UNKNOWN(), "BIOMETRIC_ERROR_STATUS_UNKNOWN"); + put(CredentialsManagerException.Companion.getBIOMETRIC_ERROR_UNSUPPORTED(), "BIOMETRIC_ERROR_UNSUPPORTED"); + put(CredentialsManagerException.Companion.getBIOMETRIC_ERROR_HW_UNAVAILABLE(), "BIOMETRIC_ERROR_HW_UNAVAILABLE"); + put(CredentialsManagerException.Companion.getBIOMETRIC_ERROR_NONE_ENROLLED(), "BIOMETRIC_ERROR_NONE_ENROLLED"); + put(CredentialsManagerException.Companion.getBIOMETRIC_ERROR_NO_HARDWARE(), "BIOMETRIC_ERROR_NO_HARDWARE"); + put(CredentialsManagerException.Companion.getBIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED(), "BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED"); + put(CredentialsManagerException.Companion.getBIOMETRIC_AUTHENTICATION_CHECK_FAILED(), "BIOMETRIC_AUTHENTICATION_CHECK_FAILED"); + put(CredentialsManagerException.Companion.getBIOMETRIC_ERROR_DEVICE_CREDENTIAL_NOT_AVAILABLE(), "BIOMETRIC_ERROR_DEVICE_CREDENTIAL_NOT_AVAILABLE"); + put(CredentialsManagerException.Companion.getBIOMETRIC_ERROR_STRONG_AND_DEVICE_CREDENTIAL_NOT_AVAILABLE(), "BIOMETRIC_ERROR_STRONG_AND_DEVICE_CREDENTIAL_NOT_AVAILABLE"); + put(CredentialsManagerException.Companion.getBIOMETRIC_ERROR_NO_DEVICE_CREDENTIAL(), "BIOMETRIC_ERROR_NO_DEVICE_CREDENTIAL"); + put(CredentialsManagerException.Companion.getBIOMETRIC_ERROR_NEGATIVE_BUTTON(), "BIOMETRIC_ERROR_NEGATIVE_BUTTON"); + put(CredentialsManagerException.Companion.getBIOMETRIC_ERROR_HW_NOT_PRESENT(), "BIOMETRIC_ERROR_HW_NOT_PRESENT"); + put(CredentialsManagerException.Companion.getBIOMETRIC_ERROR_NO_BIOMETRICS(), "BIOMETRIC_ERROR_NO_BIOMETRICS"); + put(CredentialsManagerException.Companion.getBIOMETRIC_ERROR_USER_CANCELED(), "BIOMETRIC_ERROR_USER_CANCELED"); + put(CredentialsManagerException.Companion.getBIOMETRIC_ERROR_LOCKOUT_PERMANENT(), "BIOMETRIC_ERROR_LOCKOUT_PERMANENT"); + put(CredentialsManagerException.Companion.getBIOMETRIC_ERROR_VENDOR(), "BIOMETRIC_ERROR_VENDOR"); + put(CredentialsManagerException.Companion.getBIOMETRIC_ERROR_LOCKOUT(), "BIOMETRIC_ERROR_LOCKOUT"); + put(CredentialsManagerException.Companion.getBIOMETRIC_ERROR_CANCELED(), "BIOMETRIC_ERROR_CANCELED"); + put(CredentialsManagerException.Companion.getBIOMETRIC_ERROR_NO_SPACE(), "BIOMETRIC_ERROR_NO_SPACE"); + put(CredentialsManagerException.Companion.getBIOMETRIC_ERROR_TIMEOUT(), "BIOMETRIC_ERROR_TIMEOUT"); + put(CredentialsManagerException.Companion.getBIOMETRIC_ERROR_UNABLE_TO_PROCESS(), "BIOMETRIC_ERROR_UNABLE_TO_PROCESS"); + put(CredentialsManagerException.Companion.getBIOMETRICS_INVALID_USER(), "BIOMETRICS_INVALID_USER"); + put(CredentialsManagerException.Companion.getBIOMETRIC_AUTHENTICATION_FAILED(), "BIOMETRIC_AUTHENTICATION_FAILED"); + put(CredentialsManagerException.Companion.getAPI_ERROR(), "API_ERROR"); + put(CredentialsManagerException.Companion.getNO_NETWORK(), "NO_NETWORK"); + }}; private static final String CREDENTIAL_MANAGER_ERROR_CODE = "a0.invalid_state.credential_manager_exception"; private static final String INVALID_DOMAIN_URL_ERROR_CODE = "a0.invalid_domain_url"; private static final String BIOMETRICS_AUTHENTICATION_ERROR_CODE = "a0.invalid_options_biometrics_authentication"; @@ -122,18 +159,24 @@ public void onSuccess(Credentials credentials) { @Override public void onFailure(@NonNull CredentialsManagerException e) { - promise.reject(CREDENTIAL_MANAGER_ERROR_CODE, e.getMessage(), e); + String errorCode = deduceErrorCode(e); + promise.reject(errorCode, e.getMessage(), e); } })); } + private String deduceErrorCode(@NonNull CredentialsManagerException e) { + return ERROR_CODE_MAP.getOrDefault(e, CREDENTIAL_MANAGER_ERROR_CODE); + } + @ReactMethod public void saveCredentials(ReadableMap credentials, Promise promise) { try { this.secureCredentialsManager.saveCredentials(CredentialsParser.fromMap(credentials)); promise.resolve(true); } catch (CredentialsManagerException e) { - promise.reject(CREDENTIAL_MANAGER_ERROR_CODE, e.getMessage(), e); + String errorCode = deduceErrorCode(e); + promise.reject(errorCode, e.getMessage(), e); } } diff --git a/src/credentials-manager/credentialsManagerError.ts b/src/credentials-manager/credentialsManagerError.ts index ffc651a8..8a2608a1 100644 --- a/src/credentials-manager/credentialsManagerError.ts +++ b/src/credentials-manager/credentialsManagerError.ts @@ -8,6 +8,7 @@ export default class CredentialsManagerError< public json; public status; public invalid_parameter; + public generic_error_code; constructor(response: Auth0Response) { const { status, json, text } = response; @@ -15,14 +16,17 @@ export default class CredentialsManagerError< error, error_description: description, invalid_parameter, + code, }: { error?: string; error_description?: string; invalid_parameter?: string; + code?: string; } = json ?? { error: undefined, error_description: undefined, invalid_parameter: undefined, + code: undefined, }; super( error || 'a0.response.invalid', @@ -30,14 +34,60 @@ export default class CredentialsManagerError< ); this.json = json; this.status = status; + this.generic_error_code = this.convertToCommonErrorCode(code || ''); + if (invalid_parameter) { this.invalid_parameter = invalid_parameter; } } + + convertToCommonErrorCode(code: string): string { + let errorMapping: Record = { + INVALID_CREDENTIALS: 'INVALID_CREDENTIALS', + NO_CREDENTIALS: 'NO_CREDENTIALS', + NO_REFRESH_TOKEN: 'NO_REFRESH_TOKEN', + RENEW_FAILED: 'RENEW_FAILED', + STORE_FAILED: 'STORE_FAILED', + REVOKE_FAILED: 'REVOKE_FAILED', + LARGE_MIN_TTL: 'LARGE_MIN_TTL', + INCOMPATIBLE_DEVICE: 'INCOMPATIBLE_DEVICE', + CRYPTO_EXCEPTION: 'CRYPTO_EXCEPTION', + BIOMETRIC_NO_ACTIVITY: 'BIOMETRICS_FAILED', + BIOMETRIC_ERROR_STATUS_UNKNOWN: 'BIOMETRICS_FAILED', + BIOMETRIC_ERROR_UNSUPPORTED: 'BIOMETRICS_FAILED', + BIOMETRIC_ERROR_HW_UNAVAILABLE: 'BIOMETRICS_FAILED', + BIOMETRIC_ERROR_NONE_ENROLLED: 'BIOMETRICS_FAILED', + BIOMETRIC_ERROR_NO_HARDWARE: 'BIOMETRICS_FAILED', + BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED: 'BIOMETRICS_FAILED', + BIOMETRIC_AUTHENTICATION_CHECK_FAILED: 'BIOMETRICS_FAILED', + BIOMETRIC_ERROR_DEVICE_CREDENTIAL_NOT_AVAILABLE: 'BIOMETRICS_FAILED', + BIOMETRIC_ERROR_STRONG_AND_DEVICE_CREDENTIAL_NOT_AVAILABLE: + 'BIOMETRICS_FAILED', + BIOMETRIC_ERROR_NO_DEVICE_CREDENTIAL: 'BIOMETRICS_FAILED', + BIOMETRIC_ERROR_NEGATIVE_BUTTON: 'BIOMETRICS_FAILED', + BIOMETRIC_ERROR_HW_NOT_PRESENT: 'BIOMETRICS_FAILED', + BIOMETRIC_ERROR_NO_BIOMETRICS: 'BIOMETRICS_FAILED', + BIOMETRIC_ERROR_USER_CANCELED: 'BIOMETRICS_FAILED', + BIOMETRIC_ERROR_LOCKOUT_PERMANENT: 'BIOMETRICS_FAILED', + BIOMETRIC_ERROR_VENDOR: 'BIOMETRICS_FAILED', + BIOMETRIC_ERROR_LOCKOUT: 'BIOMETRICS_FAILED', + BIOMETRIC_ERROR_CANCELED: 'BIOMETRICS_FAILED', + BIOMETRIC_ERROR_NO_SPACE: 'BIOMETRICS_FAILED', + BIOMETRIC_ERROR_TIMEOUT: 'BIOMETRICS_FAILED', + BIOMETRIC_ERROR_UNABLE_TO_PROCESS: 'BIOMETRICS_FAILED', + BIOMETRICS_INVALID_USER: 'BIOMETRICS_FAILED', + BIOMETRIC_AUTHENTICATION_FAILED: 'BIOMETRICS_FAILED', + BIOMETRICS_FAILED: 'BIOMETRICS_FAILED', + NO_NETWORK: 'NO_NETWORK', + API_ERROR: 'API_ERROR', + }; + return errorMapping[code] || 'UNKNOWN_ERROR'; + } } export interface CredentialsManagerErrorDetails { error?: string; error_description?: string; invalid_parameter?: string; + code?: string; } diff --git a/src/credentials-manager/index.ts b/src/credentials-manager/index.ts index c02a503b..4d218d18 100644 --- a/src/credentials-manager/index.ts +++ b/src/credentials-manager/index.ts @@ -52,6 +52,7 @@ class CredentialsManager { const json = { error: 'a0.credential_manager.invalid', error_description: e.message, + code: e.code, }; throw new CredentialsManagerError({ json, status: 0 }); } @@ -79,12 +80,18 @@ class CredentialsManager { this.domain, this.localAuthenticationOptions ); - return this.Auth0Module.getCredentials( - scope, - minTtl, - parameters, - forceRefresh - ); + return new Promise((resolve, reject) => { + this.Auth0Module.getCredentials(scope, minTtl, parameters, forceRefresh) + .then(resolve) + .catch((e) => { + const json = { + error: 'a0.credential_manager.invalid', + error_description: e.message, + code: e.code, + }; + reject(new CredentialsManagerError({ json, status: 0 })); + }); + }); } catch (e) { const json = { error: 'a0.credential_manager.invalid', diff --git a/src/utils/baseError.ts b/src/utils/baseError.ts index bd90bdc6..b574c184 100644 --- a/src/utils/baseError.ts +++ b/src/utils/baseError.ts @@ -1,12 +1,14 @@ class BaseError extends Error { - constructor(name: string, message: string) { + generic_error_code: string; + constructor(name: string, message: string, generic_error_code?: string) { super(); if (Error.captureStackTrace) { Error.captureStackTrace(this, this.constructor); } this.name = name; this.message = message; + this.generic_error_code = generic_error_code || ''; } } -export default BaseError; \ No newline at end of file +export default BaseError; From 1f1fe55a23c8dd70291e44083ccb2ad439e717f9 Mon Sep 17 00:00:00 2001 From: kailash-b Date: Mon, 17 Feb 2025 10:37:05 +0530 Subject: [PATCH 3/4] Added unit tests and addressed review comments --- .../java/com/auth0/react/A0Auth0Module.java | 2 +- .../__tests__/credentials-manager.spec.js | 138 ++++++++++++++++++ 2 files changed, 139 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/com/auth0/react/A0Auth0Module.java b/android/src/main/java/com/auth0/react/A0Auth0Module.java index 66eec119..aa6255e0 100644 --- a/android/src/main/java/com/auth0/react/A0Auth0Module.java +++ b/android/src/main/java/com/auth0/react/A0Auth0Module.java @@ -30,7 +30,7 @@ public class A0Auth0Module extends ReactContextBaseJavaModule implements ActivityEventListener { - public static final Map ERROR_CODE_MAP = new HashMap<>() {{ + private final Map ERROR_CODE_MAP = new HashMap<>() {{ put(CredentialsManagerException.Companion.getINVALID_CREDENTIALS(), "INVALID_CREDENTIALS"); put(CredentialsManagerException.Companion.getNO_CREDENTIALS(), "NO_CREDENTIALS"); put(CredentialsManagerException.Companion.getNO_REFRESH_TOKEN(), "NO_REFRESH_TOKEN"); diff --git a/src/credentials-manager/__tests__/credentials-manager.spec.js b/src/credentials-manager/__tests__/credentials-manager.spec.js index 73c1fd6a..2788d796 100644 --- a/src/credentials-manager/__tests__/credentials-manager.spec.js +++ b/src/credentials-manager/__tests__/credentials-manager.spec.js @@ -1,4 +1,5 @@ import CredentialsManager from '../index'; +import CredentialsManagerError from '../credentialsManagerError'; import { Platform } from 'react-native'; describe('credentials manager tests', () => { @@ -168,4 +169,141 @@ describe('credentials manager tests', () => { newNativeModule.mockRestore(); }); }); + + describe('CredentialsManagerError', () => { + describe('convertToCommonErrorCode', () => { + it('should return the correct common error code for known error codes', () => { + const error = new CredentialsManagerError({ + status: 400, + json: {}, + text: '', + }); + + expect(error.convertToCommonErrorCode('INVALID_CREDENTIALS')).toBe( + 'INVALID_CREDENTIALS' + ); + expect(error.convertToCommonErrorCode('NO_CREDENTIALS')).toBe( + 'NO_CREDENTIALS' + ); + expect(error.convertToCommonErrorCode('NO_REFRESH_TOKEN')).toBe( + 'NO_REFRESH_TOKEN' + ); + expect(error.convertToCommonErrorCode('RENEW_FAILED')).toBe( + 'RENEW_FAILED' + ); + expect(error.convertToCommonErrorCode('STORE_FAILED')).toBe( + 'STORE_FAILED' + ); + expect(error.convertToCommonErrorCode('REVOKE_FAILED')).toBe( + 'REVOKE_FAILED' + ); + expect(error.convertToCommonErrorCode('LARGE_MIN_TTL')).toBe( + 'LARGE_MIN_TTL' + ); + expect(error.convertToCommonErrorCode('INCOMPATIBLE_DEVICE')).toBe( + 'INCOMPATIBLE_DEVICE' + ); + expect(error.convertToCommonErrorCode('CRYPTO_EXCEPTION')).toBe( + 'CRYPTO_EXCEPTION' + ); + expect(error.convertToCommonErrorCode('BIOMETRIC_NO_ACTIVITY')).toBe( + 'BIOMETRICS_FAILED' + ); + expect( + error.convertToCommonErrorCode('BIOMETRIC_ERROR_STATUS_UNKNOWN') + ).toBe('BIOMETRICS_FAILED'); + expect( + error.convertToCommonErrorCode('BIOMETRIC_ERROR_UNSUPPORTED') + ).toBe('BIOMETRICS_FAILED'); + expect( + error.convertToCommonErrorCode('BIOMETRIC_ERROR_HW_UNAVAILABLE') + ).toBe('BIOMETRICS_FAILED'); + expect( + error.convertToCommonErrorCode('BIOMETRIC_ERROR_NONE_ENROLLED') + ).toBe('BIOMETRICS_FAILED'); + expect( + error.convertToCommonErrorCode('BIOMETRIC_ERROR_NO_HARDWARE') + ).toBe('BIOMETRICS_FAILED'); + expect( + error.convertToCommonErrorCode( + 'BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED' + ) + ).toBe('BIOMETRICS_FAILED'); + expect( + error.convertToCommonErrorCode( + 'BIOMETRIC_AUTHENTICATION_CHECK_FAILED' + ) + ).toBe('BIOMETRICS_FAILED'); + expect( + error.convertToCommonErrorCode( + 'BIOMETRIC_ERROR_DEVICE_CREDENTIAL_NOT_AVAILABLE' + ) + ).toBe('BIOMETRICS_FAILED'); + expect( + error.convertToCommonErrorCode( + 'BIOMETRIC_ERROR_STRONG_AND_DEVICE_CREDENTIAL_NOT_AVAILABLE' + ) + ).toBe('BIOMETRICS_FAILED'); + expect( + error.convertToCommonErrorCode('BIOMETRIC_ERROR_NO_DEVICE_CREDENTIAL') + ).toBe('BIOMETRICS_FAILED'); + expect( + error.convertToCommonErrorCode('BIOMETRIC_ERROR_NEGATIVE_BUTTON') + ).toBe('BIOMETRICS_FAILED'); + expect( + error.convertToCommonErrorCode('BIOMETRIC_ERROR_HW_NOT_PRESENT') + ).toBe('BIOMETRICS_FAILED'); + expect( + error.convertToCommonErrorCode('BIOMETRIC_ERROR_NO_BIOMETRICS') + ).toBe('BIOMETRICS_FAILED'); + expect( + error.convertToCommonErrorCode('BIOMETRIC_ERROR_USER_CANCELED') + ).toBe('BIOMETRICS_FAILED'); + expect( + error.convertToCommonErrorCode('BIOMETRIC_ERROR_LOCKOUT_PERMANENT') + ).toBe('BIOMETRICS_FAILED'); + expect(error.convertToCommonErrorCode('BIOMETRIC_ERROR_VENDOR')).toBe( + 'BIOMETRICS_FAILED' + ); + expect(error.convertToCommonErrorCode('BIOMETRIC_ERROR_LOCKOUT')).toBe( + 'BIOMETRICS_FAILED' + ); + expect(error.convertToCommonErrorCode('BIOMETRIC_ERROR_CANCELED')).toBe( + 'BIOMETRICS_FAILED' + ); + expect(error.convertToCommonErrorCode('BIOMETRIC_ERROR_NO_SPACE')).toBe( + 'BIOMETRICS_FAILED' + ); + expect(error.convertToCommonErrorCode('BIOMETRIC_ERROR_TIMEOUT')).toBe( + 'BIOMETRICS_FAILED' + ); + expect( + error.convertToCommonErrorCode('BIOMETRIC_ERROR_UNABLE_TO_PROCESS') + ).toBe('BIOMETRICS_FAILED'); + expect(error.convertToCommonErrorCode('BIOMETRICS_INVALID_USER')).toBe( + 'BIOMETRICS_FAILED' + ); + expect( + error.convertToCommonErrorCode('BIOMETRIC_AUTHENTICATION_FAILED') + ).toBe('BIOMETRICS_FAILED'); + expect(error.convertToCommonErrorCode('BIOMETRICS_FAILED')).toBe( + 'BIOMETRICS_FAILED' + ); + expect(error.convertToCommonErrorCode('NO_NETWORK')).toBe('NO_NETWORK'); + expect(error.convertToCommonErrorCode('API_ERROR')).toBe('API_ERROR'); + }); + + it('should return UNKNOWN_ERROR for unknown error codes', () => { + const error = new CredentialsManagerError({ + status: 400, + json: {}, + text: '', + }); + + expect(error.convertToCommonErrorCode('UNKNOWN_CODE')).toBe( + 'UNKNOWN_ERROR' + ); + }); + }); + }); }); From 4e14799db4df54edcc4ee9a4bb17211eed822100 Mon Sep 17 00:00:00 2001 From: kailash-b Date: Tue, 18 Feb 2025 11:56:26 +0530 Subject: [PATCH 4/4] renamed generic_error_code to 'type' --- README.md | 6 +++++- src/credentials-manager/credentialsManagerError.ts | 4 ++-- src/utils/baseError.ts | 6 +++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 50fa6afc..31f72091 100644 --- a/README.md +++ b/README.md @@ -501,10 +501,12 @@ You can access the platform agnostic generic error codes as below : try { const credentials = await auth0.credentialsManager.getCredentials(); } catch (error) { - console.log(e.generic_error_code); + console.log(e.type); } ``` +_Note_ : We have platform agnostic error codes available only for `CredentialsManagerError` as of now. + | Generic Error Code | Corresponding Error Code in Android | Corresponding Error Code in iOS | | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------- | | `INVALID_CREDENTIALS` | `INVALID_CREDENTIALS` | | @@ -517,6 +519,8 @@ try { | `INCOMPATIBLE_DEVICE` | `INCOMPATIBLE_DEVICE` | | | `CRYPTO_EXCEPTION` | `CRYPTO_EXCEPTION` | | | `BIOMETRICS_FAILED` | OneOf
`BIOMETRIC_NO_ACTIVITY`,`BIOMETRIC_ERROR_STATUS_UNKNOWN`,`BIOMETRIC_ERROR_UNSUPPORTED`,
`BIOMETRIC_ERROR_HW_UNAVAILABLE`,`BIOMETRIC_ERROR_NONE_ENROLLED`,`BIOMETRIC_ERROR_NO_HARDWARE`,
`BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED`,`BIOMETRIC_AUTHENTICATION_CHECK_FAILED`,
`BIOMETRIC_ERROR_DEVICE_CREDENTIAL_NOT_AVAILABLE` | `biometricsFailed` | +| `NO_NETWORK` | `NO_NETWORK` | | +| `API_ERROR` | `API_ERROR` | | ## Feedback diff --git a/src/credentials-manager/credentialsManagerError.ts b/src/credentials-manager/credentialsManagerError.ts index 8a2608a1..83463c87 100644 --- a/src/credentials-manager/credentialsManagerError.ts +++ b/src/credentials-manager/credentialsManagerError.ts @@ -8,7 +8,7 @@ export default class CredentialsManagerError< public json; public status; public invalid_parameter; - public generic_error_code; + public type; constructor(response: Auth0Response) { const { status, json, text } = response; @@ -34,7 +34,7 @@ export default class CredentialsManagerError< ); this.json = json; this.status = status; - this.generic_error_code = this.convertToCommonErrorCode(code || ''); + this.type = this.convertToCommonErrorCode(code || ''); if (invalid_parameter) { this.invalid_parameter = invalid_parameter; diff --git a/src/utils/baseError.ts b/src/utils/baseError.ts index b574c184..25a46809 100644 --- a/src/utils/baseError.ts +++ b/src/utils/baseError.ts @@ -1,13 +1,13 @@ class BaseError extends Error { - generic_error_code: string; - constructor(name: string, message: string, generic_error_code?: string) { + type: string; + constructor(name: string, message: string, type?: string) { super(); if (Error.captureStackTrace) { Error.captureStackTrace(this, this.constructor); } this.name = name; this.message = message; - this.generic_error_code = generic_error_code || ''; + this.type = type || ''; } }