UNPKG

web3-error-helper

Version:

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

179 lines (178 loc) 7.12 kB
"use strict"; /** * Error mapping loader * * This module handles loading and managing error mappings from JSON files * and custom chain configurations. It provides functions to load mappings * for specific chains, categories, and handles the priority-based sorting * of error patterns for optimal matching performance. */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.loadErrorMappings = loadErrorMappings; exports.loadCategoryMappings = loadCategoryMappings; exports.getAvailableCategories = getAvailableCategories; exports.hasCategoryForChain = hasCategoryForChain; exports.loadErrorCategories = loadErrorCategories; const types_1 = require("./types"); const chain_registry_1 = require("./chain-registry"); const chain_registry_2 = require("./data/chain-registry"); // Import error mappings const evmMappings = __importStar(require("./errors/evm.json")); const gasMappings = __importStar(require("./errors/gas.json")); const erc20Mappings = __importStar(require("./errors/erc20.json")); const walletMappings = __importStar(require("./errors/wallet.json")); const networkMappings = __importStar(require("./errors/network.json")); const contractMappings = __importStar(require("./errors/contract.json")); const transactionMappings = __importStar(require("./errors/transaction.json")); /** * Error category mappings */ const ERROR_CATEGORY_MAPPINGS = { erc20: erc20Mappings, gas: gasMappings, wallet: walletMappings, network: networkMappings, transaction: transactionMappings, evm: evmMappings, contract: contractMappings, }; /** * Load error mappings for a specific blockchain network * * This function loads all error mappings for the specified chain, including ERC20, gas, * wallet, network, transaction, EVM, and contract errors. Mappings are sorted by priority * (higher priority first) to ensure the most specific matches are found first. * * @param chain - The blockchain network to load mappings for. Defaults to Ethereum. * @returns Array of error mappings sorted by priority (highest first) * * @example * ```typescript * // Load Ethereum error mappings * const ethereumMappings = loadErrorMappings(SupportedChain.ETHEREUM); * * // Load Polygon error mappings * const polygonMappings = loadErrorMappings(SupportedChain.POLYGON); * * // Load custom chain mappings (falls back to Ethereum mappings) * const customMappings = loadErrorMappings('custom-chain'); * ``` */ function loadErrorMappings(chain = types_1.SupportedChain.ETHEREUM) { const allMappings = []; // Check for custom chain first if (chain_registry_1.customChainRegistry.has(chain)) { const customMappings = chain_registry_1.customChainRegistry.getErrorMappings(chain); allMappings.push(...customMappings); } // Load built-in categories based on chain configuration if (typeof chain === 'string' && Object.values(types_1.SupportedChain).includes(chain)) { const enabledCategories = (0, chain_registry_2.getEnabledErrorCategories)(chain); enabledCategories.forEach(categoryConfig => { const categoryMappings = ERROR_CATEGORY_MAPPINGS[categoryConfig.category]; if (categoryMappings) { allMappings.push(...categoryMappings.mappings); } }); } else { // Fallback to all categories for unsupported chains Object.values(ERROR_CATEGORY_MAPPINGS).forEach(category => { if (category) { allMappings.push(...category.mappings); } }); } // Sort by priority using nullish coalescing return allMappings.sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0)); } /** * Load error mappings for a specific category * * @param category - The error category to load * @param chain - The blockchain network. Defaults to Ethereum. * @returns Array of error mappings for the specified category */ function loadCategoryMappings(category, chain = types_1.SupportedChain.ETHEREUM) { // Check for custom chain first if (chain_registry_1.customChainRegistry.has(chain)) { // For custom chains, we can't filter by category since ErrorMapping doesn't have category // Return all custom mappings for now return chain_registry_1.customChainRegistry.getErrorMappings(chain); } // Load from built-in categories const categoryMappings = ERROR_CATEGORY_MAPPINGS[category]; if (categoryMappings) { return categoryMappings.mappings; } return []; } /** * Get all available error categories * * @returns Array of available error category names */ function getAvailableCategories() { return Object.keys(ERROR_CATEGORY_MAPPINGS); } /** * Check if a category exists for a specific chain * * @param category - The error category to check * @param chain - The blockchain network. Defaults to Ethereum. * @returns true if the category exists for the chain */ function hasCategoryForChain(category, _chain = types_1.SupportedChain.ETHEREUM) { const availableCategories = getAvailableCategories(); return availableCategories.includes(category); } /** * Load error categories with their mappings * * @returns Array of error categories with mappings */ function loadErrorCategories() { return [ { type: 'erc20', mappings: erc20Mappings.mappings }, { type: 'gas', mappings: gasMappings.mappings }, { type: 'wallet', mappings: walletMappings.mappings }, { type: 'network', mappings: networkMappings.mappings }, { type: 'transaction', mappings: transactionMappings.mappings }, { type: 'contract', mappings: contractMappings.mappings }, { type: 'evm', mappings: evmMappings.mappings }, ]; }