UNPKG

web3-error-helper

Version:

> 🛠️ Turn confusing Web3 errors into clear, human-friendly messages for developers and users alike.

188 lines (187 loc) 5.51 kB
/** * Custom chain registry for managing blockchain network configurations * * This module provides a registry system for managing custom blockchain networks * and their associated error mappings. It allows runtime registration of chains * with their specific error patterns and fallback messages. */ import { CustomChainConfig, ChainRegistry, ErrorMapping } from './types'; /** * In-memory registry for custom chain configurations */ declare class CustomChainRegistry implements ChainRegistry { private chains; /** * Register a custom chain configuration * * @param config - The chain configuration to register * @throws Error if chainId is already registered * * @example * ```typescript * const customChain: CustomChainConfig = { * chainId: 'my-custom-chain', * name: 'My Custom Chain', * errorMappings: [ * { pattern: 'custom error', message: 'Custom error message' } * ], * customFallbacks: { * generic: 'Custom chain error occurred' * } * }; * * registry.register(customChain); * ``` */ register(config: CustomChainConfig): void; /** * Unregister a custom chain * * @param chainId - The chain identifier to unregister * @returns true if the chain was removed, false if it wasn't found * * @example * ```typescript * const removed = registry.unregister('my-custom-chain'); * console.log(removed); // true if removed, false if not found * ``` */ unregister(chainId: string): boolean; /** * Get a custom chain configuration * * @param chainId - The chain identifier to retrieve * @returns The chain configuration or undefined if not found * * @example * ```typescript * const config = registry.get('my-custom-chain'); * if (config) { * console.log(`Found chain: ${config.name}`); * } * ``` */ get(chainId: string): CustomChainConfig | undefined; /** * Get all registered custom chains * * @returns Array of all registered chain configurations * * @example * ```typescript * const allChains = registry.getAll(); * console.log(`Registered ${allChains.length} custom chains`); * ``` */ getAll(): CustomChainConfig[]; /** * Check if a chain is registered * * @param chainId - The chain identifier to check * @returns true if the chain is registered, false otherwise * * @example * ```typescript * if (registry.has('my-custom-chain')) { * console.log('Chain is registered'); * } * ``` */ has(chainId: string): boolean; /** * Clear all registered custom chains * * @example * ```typescript * registry.clear(); * console.log('All custom chains cleared'); * ``` */ clear(): void; /** * Get error mappings for a specific chain * * @param chainId - The chain identifier * @returns Array of error mappings for the chain */ getErrorMappings(chainId: string): ErrorMapping[]; /** * Get custom fallback messages for a specific chain * * @param chainId - The chain identifier * @returns Custom fallback messages or undefined */ getCustomFallbacks(chainId: string): CustomChainConfig['customFallbacks']; /** * Validate a chain configuration * * @param config - The configuration to validate * @throws Error if the configuration is invalid */ private validateConfig; } export declare const customChainRegistry: CustomChainRegistry; /** * Register a custom chain configuration * * @param config - The chain configuration to register * * @example * ```typescript * import { registerCustomChain } from './chain-registry'; * * registerCustomChain({ * chainId: 'my-custom-chain', * name: 'My Custom Chain', * errorMappings: [ * { pattern: 'custom error', message: 'Custom error message' } * ] * }); * ``` */ export declare function registerCustomChain(config: CustomChainConfig): void; /** * Unregister a custom chain * * @param chainId - The chain identifier to unregister * @returns true if the chain was removed, false if it wasn't found */ export declare function unregisterCustomChain(chainId: string): boolean; /** * Get a custom chain configuration * * @param chainId - The chain identifier to retrieve * @returns The chain configuration or undefined if not found */ export declare function getCustomChain(chainId: string): CustomChainConfig | undefined; /** * Get all registered custom chains * * @returns Array of all registered chain configurations */ export declare function getAllCustomChains(): CustomChainConfig[]; /** * Check if a custom chain is registered * * @param chainId - The chain identifier to check * @returns true if the chain is registered, false otherwise */ export declare function hasCustomChain(chainId: string): boolean; /** * Clear all registered custom chains */ export declare function clearCustomChains(): void; /** * Get custom chain identifiers only * * @returns Array of custom chain identifiers */ export declare function getCustomChains(): string[]; /** * Check if a chain is custom * * @param chainId - The chain identifier to check * @returns true if the chain is a custom registered chain */ export declare function isCustomChain(chainId: string): boolean; export {};