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
use jsonrpc_core::{Error as JsonRpcError, ErrorCode, Value};
use jsonrpsee::types::ErrorObjectOwned;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
    #[error("Invalid params {0} {1}")]
    InvalidParams(String, String),
    #[error("Internal error: {0}")]
    InternalError(String),
}

impl From<Error> for JsonRpcError {
    fn from(e: Error) -> JsonRpcError {
        match e {
            Error::InvalidParams(msg, details) => JsonRpcError {
                code: ErrorCode::InvalidParams,
                message: msg,
                data: Some(Value::String(details)),
            },
            Error::InternalError(msg) => JsonRpcError {
                code: ErrorCode::InternalError,
                message: msg,
                data: None,
            },
        }
    }
}

impl From<Error> for ErrorObjectOwned {
    fn from(e: Error) -> ErrorObjectOwned {
        let err: JsonRpcError = e.into();
        ErrorObjectOwned::owned(err.code.code() as i32, err.message, err.data)
    }
}