1use serde::{Deserialize, Serialize};
2use thiserror::Error;
3
4#[derive(Error, Debug)]
5pub enum Error {
6 #[error("JSON-RPC error: {0}")]
7 JsonRpc(JsonRpcError),
8 #[error("Network error: {0}")]
9 Network(#[from] reqwest::Error),
10 #[error("Serialization error: {0}")]
11 Serialization(#[from] serde_json::Error),
12 #[error("Invalid parameters: {0}")]
13 InvalidParams(serde_json::Error),
14}
15
16#[derive(Serialize, Clone, Debug, Deserialize, PartialEq)]
17pub struct JsonRpcError {
18 pub code: i32,
19 pub message: String,
20 #[serde(skip_serializing_if = "Option::is_none")]
21 pub data: Option<serde_json::Value>,
22}
23
24impl std::fmt::Display for JsonRpcError {
25 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26 write!(f, "JSON-RPC error {}: {}", self.code, self.message)
27 }
28}