web3-error-helper
Version:
> 🛠️ Turn confusing Web3 errors into clear, human-friendly messages for developers and users alike.
120 lines (119 loc) • 4.09 kB
TypeScript
/**
* Error mapping utilities
*
* This module provides a comprehensive set of utility functions for manipulating,
* validating, and analyzing error mappings. It handles mapping operations such as
* merging, sorting, filtering, and validation. Supports advanced mapping analysis
* and provides tools for maintaining mapping quality and consistency.
*/
import { ErrorMapping } from './types';
/**
* Add custom error mappings to existing mappings
*
* This function allows you to add custom error mappings that will take precedence over
* default mappings. Custom mappings are given a high priority (100) to ensure they are
* matched before default mappings.
*
* @param existingMappings - The existing error mappings to extend
* @param customMappings - Object mapping error patterns to human-readable messages
* @returns Combined array of mappings with custom mappings first (highest priority)
*
* @example
* ```typescript
* const defaultMappings = loadErrorMappings(SupportedChain.ETHEREUM);
*
* const customMappings = {
* 'execution reverted: custom error': 'This is a custom error message',
* 'insufficient funds': 'You need more ETH to complete this transaction'
* };
*
* const allMappings = addCustomMappings(defaultMappings, customMappings);
* ```
*/
export declare function addCustomMappings(existingMappings: ErrorMapping[], customMappings: Record<string, string>): ErrorMapping[];
/**
* Sort error mappings by priority
*
* @param mappings - Array of error mappings to sort
* @returns Sorted array with highest priority first
*/
export declare function sortMappingsByPriority(mappings: ErrorMapping[]): ErrorMapping[];
/**
* Filter error mappings by priority
*
* @param mappings - Array of error mappings to filter
* @param minPriority - Minimum priority threshold
* @returns Filtered array of mappings
*/
export declare function filterMappingsByPriority(mappings: ErrorMapping[], minPriority: number): ErrorMapping[];
/**
* Find error mappings by pattern
*
* @param mappings - Array of error mappings to search
* @param pattern - Pattern to search for
* @param exact - Whether to match exactly or partially
* @returns Array of matching mappings
*/
export declare function findMappingsByPattern(mappings: ErrorMapping[], pattern: string, exact?: boolean): ErrorMapping[];
/**
* Find error mappings by message content
*
* @param mappings - Array of error mappings to search
* @param searchTerm - Term to search for in messages
* @returns Array of matching mappings
*/
export declare function findMappingsByMessage(mappings: ErrorMapping[], searchTerm: string): ErrorMapping[];
/**
* Get unique error patterns from mappings
*
* @param mappings - Array of error mappings
* @returns Array of unique patterns
*/
export declare function getUniquePatterns(mappings: ErrorMapping[]): string[];
/**
* Get mapping statistics
*
* @param mappings - Array of error mappings
* @returns Object with mapping statistics
*/
export declare function getMappingStats(mappings: ErrorMapping[]): {
total: number;
withPriority: number;
withoutPriority: number;
priorityRange: {
min: number;
max: number;
};
averagePriority: number;
};
/**
* Validate error mapping
*
* @param mapping - Error mapping to validate
* @returns Object with validation results
*/
export declare function validateMapping(mapping: ErrorMapping): {
isValid: boolean;
errors: string[];
};
/**
* Validate array of error mappings
*
* @param mappings - Array of error mappings to validate
* @returns Object with validation results
*/
export declare function validateMappings(mappings: ErrorMapping[]): {
isValid: boolean;
validMappings: ErrorMapping[];
invalidMappings: Array<{
mapping: ErrorMapping;
errors: string[];
}>;
};
/**
* Merge error mappings from multiple sources
*
* @param mappingSources - Array of error mapping arrays
* @returns Merged and deduplicated array of mappings
*/
export declare function mergeMappings(...mappingSources: ErrorMapping[][]): ErrorMapping[];