JSON-RPC Error Codes Reference

Complete documentation of standard error codes, custom ranges, and implementation best practices

On this page

Need validation tools?

Try our validator to check your JSON-RPC error responses.

Try Validator

Overview

In JSON-RPC, error handling is standardized through a system of error codes that provide information about what went wrong during a request. The error object structure in JSON-RPC 2.0 includes a numeric code, a short message, and an optional data field for additional details.

This reference documents both the standard error codes defined in the JSON-RPC 2.0 specification and recommended practices for creating custom error codes in your own implementations.

Error Object Structure

{
  "jsonrpc": "2.0",
  "error": {
    "code": -32700,           // Error code (number)
    "message": "Parse error",  // Short message (string)
    "data": {                  // Optional additional information
      "details": "Invalid JSON at line 1",
      "line": 1,
      "column": 34
    }
  },
  "id": null                   // Request ID or null
}

Standard Error Codes

The JSON-RPC 2.0 specification defines the following standard error codes:

CodeMessageMeaning
-32700Parse errorInvalid JSON was received by the server. An error occurred on the server while parsing the JSON text.
-32600Invalid RequestThe JSON sent is not a valid Request object.
-32601Method not foundThe method does not exist / is not available.
-32602Invalid paramsInvalid method parameter(s).
-32603Internal errorInternal JSON-RPC error.

Note: All standard error codes are negative numbers in the range -32768 to -32000. The codes -32099 to -32000 are reserved for implementation-defined server errors.

Reserved Server Error Codes

The range from -32099 to -32000 is reserved for implementation-defined server errors. Here are some common server error codes within this range:

CodeMessageMeaning
-32000Server errorGeneric server error, used when no more specific error is applicable.
-32001Server overloadedServer is currently unable to handle the request due to a temporary overload.
-32002Rate limit exceededToo many requests have been sent within a given time period.
-32003Session expiredThe session or authentication has expired.
-32004Method not readyThe method exists but is temporarily unavailable.

Implementation-Specific Errors

In addition to the standard errors, many JSON-RPC implementations use specific error codes for common scenarios. While these are not part of the official specification, they are widely recognized:

CodeMessageMeaning
-32040Invalid batch requestThe batch request contains an invalid or empty array.
-32050Content-Type errorThe Content-Type header is missing or not set to application/json.
-32060Transport errorError occurred during transport or connection.
-32070Timeout errorThe request timed out before a response was received.

Custom Error Codes

For application-specific errors, the JSON-RPC 2.0 specification allows using any error code outside the reserved range (-32768 to -32000). Custom error codes are typically chosen based on the specific needs of your application.

Recommended Ranges

RangeUsage
-32768 to -32000Reserved - Do not use (reserved for pre-defined errors)
-31999 to -1Recommended for general application errors
1 to 999Recommended for validation and input errors
1000 to 4999Recommended for business logic errors
5000+Recommended for system or infrastructure errors

Example Custom Error Codes

CodeMessageMeaning
100Invalid formatA parameter was provided in an invalid format.
101Missing fieldA required field is missing from the request.
1001UnauthorizedThe user is not authorized to perform this action.
1002Resource not foundThe requested resource could not be found.
5001Database errorAn error occurred while accessing the database.

Error Design Guidelines

When designing an error handling system for your JSON-RPC API, consider the following best practices:

Be Specific and Descriptive

Use specific error codes that clearly identify the problem. Include helpful error messages that explain what went wrong.

Use the Data Field Effectively

Include additional context in the data field to help clients understand and resolve the error.

"error": {
  "code": 101,
  "message": "Missing field",
  "data": {
    "field": "email",
    "location": "params.user",
    "description": "Email address is required"
  }
}

Document Your Error Codes

Maintain thorough documentation of all custom error codes, including their meaning and possible solutions.

Group Related Errors

Organize error codes into ranges or categories to make them more predictable and easier to handle.

Be Consistent

Use the same error code for the same error condition throughout your API. Avoid changing error codes over time.

Client-Side Error Handling

Properly handling JSON-RPC errors on the client side is critical for creating robust applications. Here's an example of how to handle errors in JavaScript:

async function callJsonRpcMethod(method, params) {
  try {
    const response = await fetch('https://api.example.com/jsonrpc', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        jsonrpc: '2.0',
        method: method,
        params: params,
        id: generateId()
      })
    });
    
    const data = await response.json();
    
    // Check for JSON-RPC error
    if (data.error) {
      switch (data.error.code) {
        // Handle standard errors
        case -32700:
          console.error('Parse error: Invalid JSON');
          break;
        case -32600:
          console.error('Invalid request');
          break;
        case -32601:
          console.error('Method not found:', method);
          break;
        case -32602:
          console.error('Invalid parameters:', params);
          break;
          
        // Handle custom application errors
        case 1001:
          console.error('Authentication required');
          // Redirect to login page
          break;
        case 1002:
          console.error('Resource not found:', data.error.data?.resource);
          break;
          
        default:
          // Generic error handler
          console.error('JSON-RPC error:', data.error.message);
      }
      
      throw new JsonRpcError(data.error.code, data.error.message, data.error.data);
    }
    
    // Return result if no error
    return data.result;
  } catch (error) {
    // Handle network or other errors
    if (!(error instanceof JsonRpcError)) {
      console.error('Network or client error:', error);
    }
    throw error;
  }
}

Error Handling Best Practices

  • Always check for the presence of an error property in the response
  • Handle both standard and custom error codes appropriately
  • Include error handling for network and transport issues
  • Log errors with sufficient context for debugging
  • Consider different handling for different error categories (validation errors vs. server errors)
  • Provide user-friendly error messages in the UI

Tip: Create a dedicated JsonRpcError class to standardize error handling throughout your application.