RequestError Class
TheRequestError class represents JSON-RPC errors that can be thrown from request handlers:
Properties
code: Numeric error code (see Error Codes below)message: Human-readable error descriptiondata: Optional additional error information
RequestError extends the standard JavaScript Error class, so it can be caught and thrown like any other error.Error Codes
The SDK provides static factory methods for standard JSON-RPC error codes:Parse Error (-32700)
Invalid JSON was received:Invalid Request (-32600)
The JSON is valid but not a proper request object:Method Not Found (-32601)
The requested method doesn’t exist or isn’t available:Invalid Params (-32602)
The method parameters are invalid:Internal Error (-32603)
An internal server error occurred:Custom ACP Errors
The SDK also includes ACP-specific error codes:Authentication Required (-32000)
Resource Not Found (-32002)
Throwing Errors
ThrowRequestError from any request handler to send a structured error response:
Catching Errors
When calling methods on the connection, catch errors to handle failures:When a request fails, the promise is rejected with the JSON-RPC
ErrorResponse object, not a RequestError instance.Error Response Type
The error response follows the JSON-RPC 2.0 specification:Automatic Error Handling
The SDK automatically handles certain errors:Zod Validation Errors
If a request handler receives invalid parameters, the SDK catches Zod validation errors and converts them toinvalidParams errors:
Unexpected Errors
If an unexpected error is thrown from a request handler, the SDK converts it to aninternalError:
Best Practices
Custom Error Codes
For application-specific errors, use custom error codes in the range-32000 to -32099 (reserved for implementation-defined errors):
Error Handling Example
Complete example showing comprehensive error handling:Related
- Extension Methods - Throwing errors in custom methods
- Connection Lifecycle - Handling connection errors