tor_rpcbase/
err.rs

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//! Error-related functionality for RPC functions.

use std::collections::HashMap;

/// Alias for a type-erased value used in an error's `data` field
type ErrorDatum = Box<dyn erased_serde::Serialize + Send + 'static>;

/// An error type returned by failing RPC methods.
#[derive(serde::Serialize)]
pub struct RpcError {
    /// A human-readable message.
    message: String,
    /// An error code inspired by json-rpc.
    #[serde(serialize_with = "ser_code")]
    code: RpcErrorKind,
    /// The ErrorKind(s) of this error.
    #[serde(serialize_with = "ser_kind")]
    kinds: AnyErrorKind,
    /// Map from namespaced keyword to related data.
    #[serde(skip_serializing_if = "Option::is_none")]
    data: Option<HashMap<String, ErrorDatum>>,
}

impl RpcError {
    /// Construct a new `RpcError` with the provided message and error code.
    pub fn new(message: String, code: RpcErrorKind) -> Self {
        Self {
            message,
            code,
            kinds: AnyErrorKind::Rpc(code),
            data: None,
        }
    }

    /// Change the declared kind of this error to `kind`.
    pub fn set_kind(&mut self, kind: tor_error::ErrorKind) {
        self.kinds = AnyErrorKind::Tor(kind);
    }

    /// Replace the `data` field named `keyword`, if any, with the object `datum`.
    ///
    /// Note that to conform with the spec, keyword must be a C identifier prefixed with a
    /// namespace, as in `rpc:missing_features`
    pub fn set_datum<D>(&mut self, keyword: String, datum: D)
    where
        D: serde::Serialize + Send + 'static,
    {
        // TODO RPC : enforce validity on `keyword`.
        self.data
            .get_or_insert_with(HashMap::new)
            .insert(keyword, Box::new(datum) as _);
    }

    /// Return true if this is an internal error.
    pub fn is_internal(&self) -> bool {
        matches!(
            self.kinds,
            AnyErrorKind::Tor(tor_error::ErrorKind::Internal)
                | AnyErrorKind::Rpc(RpcErrorKind::InternalError)
        )
    }
}

impl<T> From<T> for RpcError
where
    T: std::error::Error + tor_error::HasKind + Send + 'static,
{
    fn from(value: T) -> RpcError {
        use tor_error::ErrorReport as _;
        let message = value.report().to_string();
        let code = kind_to_code(value.kind());
        let kinds = AnyErrorKind::Tor(value.kind());
        RpcError {
            message,
            code,
            kinds,
            data: None,
        }
    }
}

/// Helper: Serialize an AnyErrorKind in RpcError.
fn ser_kind<S: serde::Serializer>(kind: &AnyErrorKind, s: S) -> Result<S::Ok, S::Error> {
    // Our spec says that `kinds` is a list.  Any tor_error::ErrorKind is prefixed with `arti:`,
    // and any RpcErrorKind is prefixed with `rpc:`

    use serde::ser::SerializeSeq;
    let mut seq = s.serialize_seq(None)?;
    match kind {
        AnyErrorKind::Tor(kind) => seq.serialize_element(&format!("arti:{:?}", kind))?,
        AnyErrorKind::Rpc(kind) => seq.serialize_element(&format!("rpc:{:?}", kind))?,
    }
    seq.end()
}

/// Helper: Serialize an RpcErrorKind as a numeric code.
fn ser_code<S: serde::Serializer>(kind: &RpcErrorKind, s: S) -> Result<S::Ok, S::Error> {
    s.serialize_i32(*kind as i32)
}

/// An ErrorKind as held by an `RpcError`
#[derive(Clone, Copy, Debug)]
enum AnyErrorKind {
    /// An ErrorKind representing a non-RPC problem.
    Tor(tor_error::ErrorKind),
    /// An ErrorKind originating within the RPC system.
    #[allow(unused)]
    Rpc(RpcErrorKind),
}

/// Error kinds for RPC errors.
///
/// Unlike `tor_error::ErrorKind`,
/// these codes do not represent a problem in an Arti function per se:
/// they are only visible to the RPC system, and should only be reported there.
///
/// For backward compatibility with json-rpc,
/// each of these codes has a unique numeric ID.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(i32)]
#[non_exhaustive]
pub enum RpcErrorKind {
    /// "The JSON sent is not a valid Request object."
    InvalidRequest = -32600,
    /// "The method does not exist."
    NoSuchMethod = -32601,
    /// "Invalid method parameter(s)."
    InvalidMethodParameters = -32602,
    /// "The server suffered some kind of internal problem"
    InternalError = -32603,
    /// "Some requested object was not valid"
    ObjectNotFound = 1,
    /// "Some other error occurred"
    RequestError = 2,
    /// This method exists, but wasn't implemented on this object.
    MethodNotImpl = 3,
    /// This request was cancelled before it could finish.
    RequestCancelled = 4,
    /// This request listed a required feature that doesn't exist.
    FeatureNotPresent = 5,
}

/// Helper: Return an error code (for backward compat with json-rpc) for an
/// ErrorKind.
///
/// These are not especially helpful and nobody should really use them.
fn kind_to_code(kind: tor_error::ErrorKind) -> RpcErrorKind {
    use tor_error::ErrorKind as EK;
    use RpcErrorKind as RC;
    match kind {
        EK::Internal | EK::BadApiUsage => RC::InternalError,
        _ => RC::RequestError, // (This is our catch-all "request error.")
    }
}

impl std::fmt::Debug for RpcError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RpcError")
            .field("message", &self.message)
            .field("code", &self.code)
            .field("kinds", &self.kinds)
            .finish()
    }
}

#[cfg(test)]
mod test {
    // @@ begin test lint list maintained by maint/add_warning @@
    #![allow(clippy::bool_assert_comparison)]
    #![allow(clippy::clone_on_copy)]
    #![allow(clippy::dbg_macro)]
    #![allow(clippy::mixed_attributes_style)]
    #![allow(clippy::print_stderr)]
    #![allow(clippy::print_stdout)]
    #![allow(clippy::single_char_pattern)]
    #![allow(clippy::unwrap_used)]
    #![allow(clippy::unchecked_duration_subtraction)]
    #![allow(clippy::useless_vec)]
    #![allow(clippy::needless_pass_by_value)]
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->

    use super::*;

    #[derive(Debug, thiserror::Error, serde::Serialize)]
    enum ExampleError {
        #[error("The {} exploded because {}", what, why)]
        SomethingExploded { what: String, why: String },

        #[error("I'm hiding the {0} in my {1}")]
        SomethingWasHidden(String, String),

        #[error("The {0} was missing")]
        SomethingWasMissing(String),

        #[error("I don't feel up to it today")]
        ProgramUnwilling,
    }

    impl tor_error::HasKind for ExampleError {
        fn kind(&self) -> tor_error::ErrorKind {
            match self {
                Self::SomethingExploded { .. } => tor_error::ErrorKind::Other,
                Self::SomethingWasHidden(_, _) => tor_error::ErrorKind::RemoteHostNotFound,
                Self::SomethingWasMissing(_) => tor_error::ErrorKind::FeatureDisabled,
                Self::ProgramUnwilling => tor_error::ErrorKind::Internal,
            }
        }
    }

    /// Assert that two json strings deserialize to equivalent objects.
    macro_rules! assert_json_eq {
        ($a:expr, $b:expr) => {
            let json_a: serde_json::Value = serde_json::from_str($a).unwrap();
            let json_b: serde_json::Value = serde_json::from_str($b).unwrap();
            assert_eq!(json_a, json_b);
        };
    }

    #[test]
    fn serialize_error() {
        let err = ExampleError::SomethingExploded {
            what: "previous implementation".into(),
            why: "worse things happen at C".into(),
        };
        let err = RpcError::from(err);
        assert_eq!(err.code, RpcErrorKind::RequestError);
        let serialized = serde_json::to_string(&err).unwrap();
        let expected_json = r#"
          {
            "message": "error: The previous implementation exploded because worse things happen at C",
            "code": 2,
            "kinds": ["arti:Other"]
         }
        "#;
        assert_json_eq!(&serialized, expected_json);

        let err = ExampleError::SomethingWasHidden(
            "zircon-encrusted tweezers".into(),
            "chrome dinette".into(),
        );
        let err = RpcError::from(err);
        let serialized = serde_json::to_string(&err).unwrap();
        let expected = r#"
        {
            "message": "error: I'm hiding the zircon-encrusted tweezers in my chrome dinette",
            "code": 2,
            "kinds": ["arti:RemoteHostNotFound"]
         }
        "#;
        assert_json_eq!(&serialized, expected);

        let err = ExampleError::SomethingWasMissing("turbo-encabulator".into());
        let err = RpcError::from(err);
        let serialized = serde_json::to_string(&err).unwrap();
        let expected = r#"
        {
            "message": "error: The turbo-encabulator was missing",
            "code": 2,
            "kinds": ["arti:FeatureDisabled"]
         }
        "#;
        assert_json_eq!(&serialized, expected);

        let err = ExampleError::ProgramUnwilling;
        let err = RpcError::from(err);
        let serialized = serde_json::to_string(&err).unwrap();
        let expected = r#"
        {
            "message": "error: I don't feel up to it today",
            "code": -32603,
            "kinds": ["arti:Internal"]
         }
        "#;
        assert_json_eq!(&serialized, expected);
    }

    #[test]
    fn create_error() {
        let mut e = RpcError::new("Example error".to_string(), RpcErrorKind::RequestError);
        e.set_kind(tor_error::ErrorKind::CacheCorrupted);
        e.set_datum("rpc:example".to_string(), "Hello world".to_string());
        let serialized = serde_json::to_string(&e).unwrap();
        let expected = r#"
        {
            "message": "Example error",
            "code": 2,
            "kinds": ["arti:CacheCorrupted"],
            "data": {
                "rpc:example": "Hello world"
            }
        }
        "#;
        assert_json_eq!(&serialized, expected);
    }
}