Skip to main content

json_rpc/
error.rs

1//! Error types for the JSON-RPC implementation.
2//!
3//! This module defines internal errors that can occur during JSON-RPC processing,
4//! distinct from the JSON-RPC wire format errors defined in the `types` module.
5
6/// Internal errors that can occur during JSON-RPC processing.
7///
8/// These are implementation-level errors, separate from the JSON-RPC protocol
9/// error objects that are sent over the wire (defined in `types::Error`).
10#[derive(Debug, thiserror::Error)]
11pub enum Error {
12    /// Protocol-level error, such as invalid method or parameters.
13    #[error("Protocol error: {0}")]
14    ProtocolError(String),
15
16    /// JSON-RPC error with specific code and message.
17    #[error("JSON-RPC error: code={code}, message={message}")]
18    RpcError { code: i32, message: String },
19
20    /// JSON parsing error.
21    #[error("Protocol error: {0}")]
22    ParseError(#[from] serde_json::Error),
23
24    /// Invalid JSON-RPC request error.
25    #[error("Invalid Request: {0}")]
26    InvalidRequest(String),
27}
28
29impl Error {
30    /// Create a new protocol error.
31    pub fn protocol(message: impl Into<String>) -> Self {
32        Self::ProtocolError(message.into())
33    }
34
35    /// Create a new JSON-RPC error with a specific code and message.
36    pub fn rpc(code: i32, message: impl Into<String>) -> Self {
37        Self::RpcError {
38            code,
39            message: message.into(),
40        }
41    }
42
43    /// Create a new Invalid Request error.
44    pub fn invalid_request(message: impl Into<String>) -> Self {
45        Self::InvalidRequest(message.into())
46    }
47}