rpc_router/rpc_message/
rpc_request_parsing_error.rs

1use serde::Serialize;
2use serde_json::Value;
3use serde_with::{DisplayFromStr, serde_as};
4
5/// The RPC Request Parsing error is used when utilizing `value.try_into()?` or `Request::from_value(value)`.
6/// The design intent is to validate and provide as much context as possible when a specific validation fails.
7///
8/// Note: By design, we do not capture the "params" because they could be indefinitely large.
9///
10/// Note: In future releases, the capture of Value objects or arrays for those error variants
11///       will be replaced with Value::String containing a message such as
12///       `"[object/array redacted, 'id' must be of type number, string, or equal to null]"`
13///       or `"[object/array redacted, 'method' must be of type string]"`
14///       This approach aims to provide sufficient context for debugging the issue while preventing
15///       the capture of indefinitely large values in the logs.
16#[serde_as]
17#[derive(Debug, Serialize)]
18pub enum RpcRequestParsingError {
19	RequestInvalidType {
20		actual_type: String,
21	},
22
23	ParamsInvalidType {
24		actual_type: String,
25	},
26
27	VersionMissing {
28		id: Option<Value>, // Keep Value here as RpcId parsing might not have happened yet
29		method: Option<String>,
30	},
31	VersionInvalid {
32		id: Option<Value>, // Keep Value here
33		method: Option<String>,
34		version: Value,
35	},
36
37	MethodMissing {
38		id: Option<Value>, // Keep Value here
39	},
40	MethodInvalidType {
41		id: Option<Value>, // Keep Value here
42		method: Value,
43	},
44
45	NotificationHasId {
46		method: Option<String>,
47		id: Value,
48	},
49
50	MethodInvalid {
51		actual: String,
52	},
53
54	IdMissing {
55		method: Option<String>,
56	},
57	IdInvalid {
58		actual: String,
59		cause: String,
60	},
61
62	Parse(#[serde_as(as = "DisplayFromStr")] serde_json::Error), // Generic serde error if basic JSON is invalid
63}
64
65impl core::fmt::Display for RpcRequestParsingError {
66	fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::result::Result<(), core::fmt::Error> {
67		write!(fmt, "{self:?}")
68	}
69}
70
71impl std::error::Error for RpcRequestParsingError {}