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

            
3
/// An error encountered while parsing an RPC request.
4
2
#[derive(Clone, Debug, thiserror::Error, serde::Serialize)]
5
pub(crate) enum RequestParseError {
6
    /// The provided object was not well-formed json.
7
    #[error("Error in json syntax.")]
8
    InvalidJson,
9

            
10
    /// Received something that was json, but not a json object.
11
    #[error("Received something other than a json object.")]
12
    NotAnObject,
13

            
14
    /// The `id` field was missing.
15
    #[error("Request did not have any `id` field.")]
16
    IdMissing,
17

            
18
    /// The `id` field did not have the expected type (integer or string).
19
    #[error("Request's `id` field was not an integer or a string.")]
20
    IdType,
21

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

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

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

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

            
38
    /// The `meta` field was present, but it could not be parsed.
39
    ///
40
    /// Maybe it was not a json object; maybe it had a field of the wrong type.
41
    #[error("Request's `meta` field was not valid.")]
42
    MetaType,
43

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

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

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

            
57
impl tor_error::HasKind for RequestParseError {
58
4
    fn kind(&self) -> tor_error::ErrorKind {
59
4
        use tor_error::ErrorKind as EK;
60
4

            
61
4
        match self {
62
            Self::InvalidJson
63
            | Self::NotAnObject
64
            | Self::IdMissing
65
            | Self::IdType
66
            | Self::ObjMissing
67
            | Self::ObjType
68
            | Self::MethodMissing
69
            | Self::MethodType
70
            | Self::MetaType
71
4
            | Self::MissingParams => EK::RpcInvalidRequest,
72
            Self::MethodNotFound => EK::RpcMethodNotFound,
73
            Self::ParamType => EK::RpcInvalidMethodParameters,
74
        }
75
4
    }
76
}