import { type MpcErrorData } from '../../../types' export enum MpcErrorCodes { CLIENT_NOT_VERIFIED = 'CLIENT_NOT_VERIFIED', FORMAT_SHARES_ERROR = 'FORMAT_SHARES_ERROR', GOOGLE_UNAUTHENTICATED = 'GOOGLE_UNAUTHENTICATED', KEYCHAIN_UNAVAILABLE = 'KEYCHAIN_UNAVAILABLE', MPC_MODULE_NOT_FOUND = 'MPC_MODULE_NOT_FOUND', PASSWORD_REQUIRED = 'PASSWORD_REQUIRED', STORAGE_UNAVAILABLE = 'STORAGE_UNAVAILABLE', UNABLE_TO_READ_SIGNING_STORAGE = 'UNABLE_TO_READ_SIGNING_STORAGE', UNEXPECTED_ERROR = 'UNEXPECTED_ERROR', UNSUPPORTED_MPC_VERSION = 'UNSUPPORTED_MPC_VERSION', UNSUPPORTED_STORAGE_METHOD = 'UNSUPPORTED_STORAGE_METHOD', WALLET_MODIFICATION_ALREADY_IN_PROGRESS = 'WALLET_MODIFICATION_ALREADY_IN_PROGRESS', } const MpcErrorMetadata: Record = { [MpcErrorCodes.CLIENT_NOT_VERIFIED]: { id: MpcErrorCodes.CLIENT_NOT_VERIFIED, /** * @deprecated Use `id` instead */ code: MpcErrorCodes.CLIENT_NOT_VERIFIED, message: () => `[PortalMpc] Unable to verify client`, }, [MpcErrorCodes.FORMAT_SHARES_ERROR]: { id: MpcErrorCodes.FORMAT_SHARES_ERROR, /** * @deprecated Use `id` instead */ code: MpcErrorCodes.FORMAT_SHARES_ERROR, message: () => `[PortalMpc] Error formatting shares`, }, [MpcErrorCodes.GOOGLE_UNAUTHENTICATED]: { id: MpcErrorCodes.GOOGLE_UNAUTHENTICATED, /** * @deprecated Use `id` instead */ code: MpcErrorCodes.GOOGLE_UNAUTHENTICATED, message: () => `[PortalMpc] Unable to authenticate with Google`, }, [MpcErrorCodes.PASSWORD_REQUIRED]: { id: MpcErrorCodes.PASSWORD_REQUIRED, /** * @deprecated Use `id` instead */ code: MpcErrorCodes.PASSWORD_REQUIRED, message: () => `[PortalMpc] Password is required in backupConfig for password storage.`, }, [MpcErrorCodes.KEYCHAIN_UNAVAILABLE]: { id: MpcErrorCodes.KEYCHAIN_UNAVAILABLE, /** * @deprecated Use `id` instead */ code: MpcErrorCodes.KEYCHAIN_UNAVAILABLE, message: () => `[PortalMpc] Keychain is unavailable`, }, [MpcErrorCodes.MPC_MODULE_NOT_FOUND]: { id: MpcErrorCodes.MPC_MODULE_NOT_FOUND, /** * @deprecated Use `id` instead */ code: MpcErrorCodes.MPC_MODULE_NOT_FOUND, message: () => `[PortalMpc] The MPC module could not be found. This is usually an issue with React Native linking. Please verify that the 'PortalReactNative' module is properly linked to this project.`, }, [MpcErrorCodes.STORAGE_UNAVAILABLE]: { id: MpcErrorCodes.STORAGE_UNAVAILABLE, /** * @deprecated Use `id` instead */ code: MpcErrorCodes.STORAGE_UNAVAILABLE, message: () => `[PortalMpc] Storage is unavailable`, }, [MpcErrorCodes.UNABLE_TO_READ_SIGNING_STORAGE]: { id: MpcErrorCodes.UNABLE_TO_READ_SIGNING_STORAGE, /** * @deprecated Use `id` instead */ code: MpcErrorCodes.UNABLE_TO_READ_SIGNING_STORAGE, message: () => `[PortalMpc] Unable to read signing share from storage`, }, [MpcErrorCodes.UNEXPECTED_ERROR]: { id: MpcErrorCodes.UNEXPECTED_ERROR, /** * @deprecated Use `id` instead */ code: MpcErrorCodes.UNEXPECTED_ERROR, message: (error?: string) => `[PortalMpc] Unexpected error${error ? ` on ${error}` : ''}`, }, [MpcErrorCodes.UNSUPPORTED_MPC_VERSION]: { id: MpcErrorCodes.UNSUPPORTED_MPC_VERSION, /** * @deprecated Use `id` instead */ code: MpcErrorCodes.UNSUPPORTED_MPC_VERSION, message: (version?: string) => `[PortalMpc] Unsupported MPC version ${version}`, }, [MpcErrorCodes.UNSUPPORTED_STORAGE_METHOD]: { id: MpcErrorCodes.UNSUPPORTED_STORAGE_METHOD, /** * @deprecated Use `id` instead */ code: MpcErrorCodes.UNSUPPORTED_STORAGE_METHOD, message: (method?: string) => `[PortalMpc] Unsupported storage method ${method}`, }, [MpcErrorCodes.WALLET_MODIFICATION_ALREADY_IN_PROGRESS]: { id: MpcErrorCodes.WALLET_MODIFICATION_ALREADY_IN_PROGRESS, /** * @deprecated Use `id` instead */ code: MpcErrorCodes.WALLET_MODIFICATION_ALREADY_IN_PROGRESS, message: () => `[PortalMpc] Wallet modification already in progress`, }, } export class MpcError extends Error { public code: string public id?: string public context?: string /** * @param code - MpcErrorCodes * @param context - additional message or context * @param id - optional external error identifier (for example, from a backend) */ constructor(code: MpcErrorCodes, context?: string, id?: string) { const error = MpcErrorMetadata[code] // Init the actual error super(error.message(context)) // Custom error context this.code = error.code if (context) { this.context = context } this.id = id ?? error.id } } export default MpcError