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
use std::fmt;

use serde::{Deserialize, Serialize};

/// A serializable error type for use in RPC responses.
#[derive(Serialize, Deserialize, Debug, thiserror::Error)]
pub struct RpcError(serde_error::Error);

impl fmt::Display for RpcError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.0, f)
    }
}

impl From<anyhow::Error> for RpcError {
    fn from(e: anyhow::Error) -> Self {
        RpcError(serde_error::Error::new(&*e))
    }
}

impl From<std::io::Error> for RpcError {
    fn from(e: std::io::Error) -> Self {
        RpcError(serde_error::Error::new(&e))
    }
}

impl std::clone::Clone for RpcError {
    fn clone(&self) -> Self {
        RpcError(serde_error::Error::new(self))
    }
}

/// A serializable result type for use in RPC responses.
pub type RpcResult<T> = std::result::Result<T, RpcError>;