1
//!  Error types used in by `arti-rpcserver`].
2

            
3
use tor_rpcbase::RpcError;
4

            
5
/// 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)]
11
pub enum RequestParseError {
12
    /// The `id` field was missing.
13
    #[error("Request did not have any `id` field.")]
14
    IdMissing,
15

            
16
    /// 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.")]
18
    IdType,
19

            
20
    /// The `obj` field was missing.
21
    #[error("Request did not have any `obj` field.")]
22
    ObjMissing,
23

            
24
    /// The `method` field did not have the expected type (string).
25
    #[error("Request's `obj` field was not a string.")]
26
    ObjType,
27

            
28
    /// The `method` field was missing.
29
    #[error("Request had no `method` field.")]
30
    MethodMissing,
31

            
32
    /// The `method` field did not have the expected type (string).
33
    #[error("Request's `method` field was not a string.")]
34
    MethodType,
35

            
36
    /// 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.")]
40
    MetaType,
41

            
42
    /// The `method` field was not the name of any recognized method.
43
    #[error("Request's `method` field was unrecognized")]
44
    NoSuchMethod,
45

            
46
    /// The parameters were of the wrong type for the method.
47
    #[error("Parameter types incorrect for specified method")]
48
    ParamType,
49

            
50
    /// The `params` field was missing.
51
    #[error("Request's `params` field was missing.")]
52
    MissingParams,
53
}
54

            
55
impl From<RequestParseError> for RpcError {
56
2
    fn from(err: RequestParseError) -> Self {
57
        use tor_rpcbase::RpcErrorKind as EK;
58
        use RequestParseError as E;
59
2
        let 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
2
            | E::MissingParams => EK::InvalidRequest,
68
            E::NoSuchMethod => EK::NoSuchMethod,
69
            E::ParamType => EK::InvalidMethodParameters,
70
        };
71
2
        RpcError::new(err.to_string(), kind)
72
2
    }
73
}