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
//! Error conversions for Zebra's RPC methods.

use jsonrpc_core::ErrorCode;

pub(crate) trait MapServerError<T, E> {
    fn map_server_error(self) -> std::result::Result<T, jsonrpc_core::Error>;
}

pub(crate) trait OkOrServerError<T> {
    fn ok_or_server_error<S: ToString>(
        self,
        message: S,
    ) -> std::result::Result<T, jsonrpc_core::Error>;
}

impl<T, E> MapServerError<T, E> for Result<T, E>
where
    E: ToString,
{
    fn map_server_error(self) -> Result<T, jsonrpc_core::Error> {
        self.map_err(|error| jsonrpc_core::Error {
            code: ErrorCode::ServerError(0),
            message: error.to_string(),
            data: None,
        })
    }
}

impl<T> OkOrServerError<T> for Option<T> {
    fn ok_or_server_error<S: ToString>(self, message: S) -> Result<T, jsonrpc_core::Error> {
        self.ok_or(jsonrpc_core::Error {
            code: ErrorCode::ServerError(0),
            message: message.to_string(),
            data: None,
        })
    }
}