Core helper utilities providing standardized error handling and data formatting for the Web3.js ecosystem
—
Complete error handling system providing standardized error creation for connection issues, validation failures, transaction problems, and contract interactions. All error functions return Error objects with additional metadata properties.
Errors related to provider connections and network issues.
/**
* Creates error from JSON-RPC error response
* @param result - Error result from JSON-RPC response
* @returns Error with formatted message and data property
*/
function ErrorResponse(result: Error): Error;
/**
* Creates connection failure error
* @param host - Host that failed to connect
* @param event - Optional WebSocket event details
* @returns ConnectionError with code and reason properties
*/
function InvalidConnection(host: string, event?: WebSocketEvent): ConnectionError;
/**
* Creates invalid provider error
* @returns Error indicating provider is not set or invalid
*/
function InvalidProvider(): Error;
/**
* Creates invalid JSON-RPC response error
* @param result - Invalid response that caused the error
* @returns Error with formatted message
*/
function InvalidResponse(result: Error): Error;
/**
* Creates connection timeout error
* @param ms - Timeout duration that was exceeded
* @returns Error indicating connection timeout
*/
function ConnectionTimeout(ms: string): Error;
/**
* Creates connection not open error
* @returns Error indicating connection is not open for sending
*/
function ConnectionNotOpenError(): Error;
/**
* Creates connection close error
* @param event - WebSocket close event or boolean
* @returns Error or ConnectionError with close details
*/
function ConnectionCloseError(event: WebSocketEvent | boolean): Error | ConnectionError;
/**
* Creates max reconnection attempts error
* @returns Error indicating maximum reconnection attempts reached
*/
function MaxAttemptsReachedOnReconnectingError(): Error;
/**
* Creates pending requests during reconnection error
* @returns Error indicating reconnection started with pending requests
*/
function PendingRequestsOnReconnectingError(): Error;
/**
* Creates generic connection error
* @param msg - Error message
* @param event - Optional WebSocket event details
* @returns ConnectionError with code and reason from event
*/
function ConnectionError(msg: string, event?: WebSocketEvent): ConnectionError;Usage Examples:
const { errors } = require("web3-core-helpers");
// Connection failures
const connError = errors.InvalidConnection("wss://localhost:8546");
console.log(connError.message); // "CONNECTION ERROR: Couldn't connect to node wss://localhost:8546."
// Timeout errors
const timeoutError = errors.ConnectionTimeout("30000");
console.log(timeoutError.message); // "CONNECTION TIMEOUT: timeout of 30000 ms achived"
// Provider issues
const providerError = errors.InvalidProvider();
console.log(providerError.message); // "Provider not set or invalid"Errors for parameter count and validation issues.
/**
* Creates parameter count validation error
* @param got - Number of parameters received
* @param expected - Number of parameters expected
* @param method - Method name that failed validation
* @returns Error with parameter count details
*/
function InvalidNumberOfParams(got: number, expected: number, method: string): Error;Usage Example:
const paramError = errors.InvalidNumberOfParams(2, 3, "eth_getBalance");
console.log(paramError.message); // 'Invalid number of parameters for "eth_getBalance". Got 2 expected 3!'Errors related to transaction execution and smart contract interactions.
/**
* Creates smart contract revert error
* @param reason - Revert reason string
* @param signature - Method signature that reverted
* @returns RevertInstructionError with reason and signature properties
*/
function RevertInstructionError(reason: string, signature: string): RevertInstructionError;
/**
* Creates transaction revert error with receipt
* @param reason - Revert reason string
* @param signature - Method signature that reverted
* @param receipt - Transaction receipt object
* @returns TransactionRevertInstructionError with reason, signature, and receipt
*/
function TransactionRevertInstructionError(reason: string, signature: string, receipt: object): TransactionRevertInstructionError;
/**
* Creates generic transaction error
* @param message - Error message
* @param receipt - Transaction receipt object
* @returns TransactionError with receipt property
*/
function TransactionError(message: string, receipt: object): TransactionError;
/**
* Creates no contract address found error
* @param receipt - Transaction receipt without contract address
* @returns TransactionError indicating missing contract address
*/
function NoContractAddressFoundError(receipt: object): TransactionError;
/**
* Creates contract code not stored error
* @param receipt - Transaction receipt from failed deployment
* @returns TransactionError indicating code storage failure
*/
function ContractCodeNotStoredError(receipt: object): TransactionError;
/**
* Creates transaction reverted without reason error
* @param receipt - Transaction receipt from reverted transaction
* @returns TransactionError for revert without reason string
*/
function TransactionRevertedWithoutReasonError(receipt: object): TransactionError;
/**
* Creates out of gas transaction error
* @param receipt - Transaction receipt from out-of-gas transaction
* @returns TransactionError indicating insufficient gas
*/
function TransactionOutOfGasError(receipt: object): TransactionError;Usage Examples:
// Smart contract revert
const revertError = errors.RevertInstructionError("Insufficient balance", "transfer(address,uint256)");
console.log(revertError.reason); // "Insufficient balance"
console.log(revertError.signature); // "transfer(address,uint256)"
// Transaction errors with receipt
const receipt = { transactionHash: "0x...", gasUsed: "0x5208" };
const gasError = errors.TransactionOutOfGasError(receipt);
console.log(gasError.receipt); // Contains the receipt objectErrors specific to contract instantiation and interaction.
/**
* Creates ENS resolver method missing error
* @param address - Resolver contract address
* @param name - Missing method name
* @returns Error indicating resolver method not implemented
*/
function ResolverMethodMissingError(address: string, name: string): Error;
/**
* Creates contract missing ABI error
* @returns Error indicating contract ABI not provided
*/
function ContractMissingABIError(): Error;
/**
* Creates contract once requires callback error
* @returns Error indicating missing callback for once method
*/
function ContractOnceRequiresCallbackError(): Error;
/**
* Creates contract event does not exist error
* @param eventName - Name of non-existent event
* @returns Error indicating event not found in contract
*/
function ContractEventDoesNotExistError(eventName: string): Error;
/**
* Creates contract reserved event error
* @param type - Reserved event name that was used
* @returns Error indicating reserved event name usage
*/
function ContractReservedEventError(type: string): Error;
/**
* Creates contract missing deploy data error
* @returns Error indicating missing deployment data
*/
function ContractMissingDeployDataError(): Error;
/**
* Creates contract no address defined error
* @returns Error indicating contract address not set
*/
function ContractNoAddressDefinedError(): Error;
/**
* Creates contract no from address defined error
* @returns Error indicating missing from address
*/
function ContractNoFromAddressDefinedError(): Error;Usage Examples:
// Contract ABI missing
const abiError = errors.ContractMissingABIError();
console.log(abiError.message); // "You must provide the json interface of the contract when instantiating a contract object."
// Event not found
const eventError = errors.ContractEventDoesNotExistError("NonExistentEvent");
console.log(eventError.message); // 'Event "NonExistentEvent" doesn\'t exist in this contract.'
// Reserved event name
const reservedError = errors.ContractReservedEventError("newListener");
console.log(reservedError.message); // 'The event "newListener" is a reserved event name, you can\'t use it.'interface ConnectionError extends Error {
code: string | undefined;
reason: string | undefined;
}
interface TransactionError extends Error {
receipt: object;
}
interface RevertInstructionError extends Error {
reason: string;
signature: string;
}
interface TransactionRevertInstructionError extends Error {
reason: string;
signature: string;
receipt: object;
}
interface WebSocketEvent {
code?: number;
reason?: string;
}Install with Tessl CLI
npx tessl i tessl/npm-web3-core-helpers