Skip to main content
JSON-RPC error type and standardized error codes following JSON-RPC 2.0 and EIP-1474 specifications.

Type Definition

export interface JsonRpcErrorType {
  readonly code: number;
  readonly message: string;
  readonly data?: unknown;
}

Quick Start

import {
  JsonRpcError,
  INVALID_INPUT,
  RESOURCE_NOT_FOUND,
} from '@tevm/voltaire/JsonRpcError';

// Create error
const error = JsonRpcError.from(INVALID_INPUT, 'execution reverted');

// With additional data
const revertError = JsonRpcError.from(
  INVALID_INPUT,
  'execution reverted',
  '0x08c379a0...' // ABI-encoded revert reason
);

// Format error
const formatted = JsonRpcError.toString(error);
// "[-32000] execution reverted"

Error Code Constants

Standard JSON-RPC 2.0

CodeConstantDescription
-32700PARSE_ERRORInvalid JSON received by server
-32600INVALID_REQUESTJSON is not a valid request object
-32601METHOD_NOT_FOUNDMethod does not exist or is not available
-32602INVALID_PARAMSInvalid method parameter(s)
-32603INTERNAL_ERRORInternal JSON-RPC error

Ethereum-Specific (EIP-1474)

Server error range: -32000 to -32099
CodeConstantDescription
-32000INVALID_INPUTMissing or invalid parameters (commonly “execution reverted”)
-32001RESOURCE_NOT_FOUNDRequested resource not found (block, transaction, etc.)
-32002RESOURCE_UNAVAILABLERequested resource not available (node syncing, data not ready)
-32003TRANSACTION_REJECTEDTransaction creation failed
-32004METHOD_NOT_SUPPORTEDMethod exists but is not implemented
-32005LIMIT_EXCEEDEDRequest exceeds defined limit
-32006JSON_RPC_VERSION_NOT_SUPPORTEDJSON-RPC protocol version not supported

API Reference

from()

Create error from code and message:
// From code and message
const err1 = JsonRpcError.from(-32000, 'Invalid input');

// With data
const err2 = JsonRpcError.from(-32000, 'execution reverted', '0x...');

// From error object
const err3 = JsonRpcError.from({
  code: -32000,
  message: 'Invalid input',
  data: { reason: 'insufficient gas' }
});
Parameters:
  • code: number | JsonRpcErrorType - Error code or error object
  • message?: string - Error message
  • data?: unknown - Additional error data
Returns: JsonRpcErrorType

toString()

Format error as string:
const error = JsonRpcError.from(-32000, 'execution reverted');
const formatted = JsonRpcError.toString(error);
// "[-32000] execution reverted"
Parameters:
  • error: JsonRpcErrorType - Error object
Returns: string - Formatted error string

Common Patterns

Execution Reverted

The -32000 error code is most common for contract execution failures:
import { INVALID_INPUT } from '@tevm/voltaire/JsonRpcError';

// Contract call failed
const error = JsonRpcError.from(
  INVALID_INPUT,
  'execution reverted',
  '0x08c379a0...' // ABI-encoded revert reason
);

// Check for execution revert
if (response.error?.code === INVALID_INPUT) {
  console.error('Contract reverted:', response.error.data);
}

Error Code Checking

import {
  INVALID_INPUT,
  RESOURCE_NOT_FOUND,
  METHOD_NOT_FOUND,
} from '@tevm/voltaire/JsonRpcError';

function handleError(error: JsonRpcErrorType) {
  switch (error.code) {
    case INVALID_INPUT:
      return 'Invalid input or execution reverted';
    case RESOURCE_NOT_FOUND:
      return 'Resource not found';
    case METHOD_NOT_FOUND:
      return 'Method not supported';
    default:
      return `Error ${error.code}: ${error.message}`;
  }
}

Error Messages Lookup

import { ERROR_MESSAGES } from '@tevm/voltaire/JsonRpcError';

const code = -32000;
const defaultMessage = ERROR_MESSAGES[code]; // "Invalid input"

Tree-Shaking

Import only what you need:
// Import specific constants (tree-shakeable)
import {
  INVALID_INPUT,
  RESOURCE_NOT_FOUND,
  TRANSACTION_REJECTED,
} from '@tevm/voltaire/JsonRpcError';

// Import constructors
import { from, toString } from '@tevm/voltaire/JsonRpcError';

Specifications