1//! Error types used in by `arti-rpcserver`].
23use tor_rpcbase::RpcError;
45/// An error encountered while parsing an RPC request,
6/// that we will report to the client.
7///
8/// Note that this does not include fatal parsing errors
9/// that result in closing the connection entirely.
10#[derive(Clone, Debug, thiserror::Error, serde::Serialize)]
11pub enum RequestParseError {
12/// The `id` field was missing.
13#[error("Request did not have any `id` field.")]
14IdMissing,
1516/// The `id` field did not have the expected type (integer or string).
17#[error("Request's `id` field was not an integer or a string.")]
18IdType,
1920/// The `obj` field was missing.
21#[error("Request did not have any `obj` field.")]
22ObjMissing,
2324/// The `method` field did not have the expected type (string).
25#[error("Request's `obj` field was not a string.")]
26ObjType,
2728/// The `method` field was missing.
29#[error("Request had no `method` field.")]
30MethodMissing,
3132/// The `method` field did not have the expected type (string).
33#[error("Request's `method` field was not a string.")]
34MethodType,
3536/// The `meta` field was present, but it could not be parsed.
37 ///
38 /// Maybe it was not a json object; maybe it had a field of the wrong type.
39#[error("Request's `meta` field was not valid.")]
40MetaType,
4142/// The `method` field was not the name of any recognized method.
43#[error("Request's `method` field was unrecognized")]
44NoSuchMethod,
4546/// The parameters were of the wrong type for the method.
47#[error("Parameter types incorrect for specified method")]
48ParamType,
4950/// The `params` field was missing.
51#[error("Request's `params` field was missing.")]
52MissingParams,
53}
5455impl From<RequestParseError> for RpcError {
56fn from(err: RequestParseError) -> Self {
57use tor_rpcbase::RpcErrorKind as EK;
58use RequestParseError as E;
59let kind = match err {
60 E::IdMissing
61 | E::IdType
62 | E::ObjMissing
63 | E::ObjType
64 | E::MethodMissing
65 | E::MethodType
66 | E::MetaType
67 | E::MissingParams => EK::InvalidRequest,
68 E::NoSuchMethod => EK::NoSuchMethod,
69 E::ParamType => EK::InvalidMethodParameters,
70 };
71 RpcError::new(err.to_string(), kind)
72 }
73}