import type { RpcErrorDefinition, RpcErrorOptions, ValidRpcErrorCodes, } from '../../../types' export enum RpcErrorCodes { UserRejectedRequest = 4001, Unauthorized = 4100, UnsupportedMethod = 4200, Disconnected = 4900, ChainDisconnected = 4901, } const providerErrors = { 4001: { name: 'User Rejected Request', description: 'The user rejected the request.', }, 4100: { name: 'Unauthorized', description: 'The requested method and/or account has not been authorized by the user.', }, 4200: { name: 'Unsupported Method', description: 'The Provider does not support the requested method.', }, 4900: { name: 'Disconnected', description: 'The Provider is disconnected from all chains.', }, 4901: { name: 'Chain Disconnected', description: 'The Provider is not connected to the requested chain.', }, } as Record class ProviderRpcError extends Error { public code: ValidRpcErrorCodes public definition?: RpcErrorDefinition public data?: unknown constructor({ code, data }: RpcErrorOptions) { const log = console const definition = providerErrors[code as ValidRpcErrorCodes] if (!definition) { log.warn( `[Portal] ProviderRpcError instantiated with unknown error code ${code}. Attempting to continue with limited context.`, ) } const message = definition ? `${definition.name} - ${definition.description}` : '' super(`[Portal] RPC Error: ${code} ${message}`) // Set the prototype explicitly. Object.setPrototypeOf(this, ProviderRpcError.prototype) this.code = code this.data = data this.definition = definition } } export default ProviderRpcError