summaryrefslogtreecommitdiff
path: root/crates/arti-rpcserver/src/err.rs
blob: a3702dcb4a0ce93cb152e25b3cf73b766d3d8e4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//!  Error types used in by `arti-rpcserver`].

use tor_rpcbase::RpcError;

/// An error encountered while parsing an RPC request,
/// that we will report to the client.
///
/// Note that this does not include fatal parsing errors
/// that result in closing the connection entirely.
#[derive(Clone, Debug, thiserror::Error, serde::Serialize)]
pub enum RequestParseError {
    /// The `id` field was missing.
    #[error("Request did not have any `id` field.")]
    IdMissing,

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

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

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

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

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

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

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

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

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

impl From<RequestParseError> for RpcError {
    fn from(err: RequestParseError) -> Self {
        use RequestParseError as E;
        use tor_rpcbase::RpcErrorKind as EK;
        let kind = match err {
            E::IdMissing
            | E::IdType
            | E::ObjMissing
            | E::ObjType
            | E::MethodMissing
            | E::MethodType
            | E::MetaType
            | E::MissingParams => EK::InvalidRequest,
            E::NoSuchMethod => EK::NoSuchMethod,
            E::ParamType => EK::InvalidMethodParameters,
        };
        RpcError::new(err.to_string(), kind)
    }
}