cfx_rpc_eth_types/
errors.rs

1use jsonrpc_core::{Error as JsonRpcError, ErrorCode, Value};
2use jsonrpsee::types::ErrorObjectOwned;
3use thiserror::Error;
4
5#[derive(Debug, Error)]
6pub enum Error {
7    #[error("Invalid params {0} {1}")]
8    InvalidParams(String, String),
9    #[error("Internal error: {0}")]
10    InternalError(String),
11}
12
13impl From<Error> for JsonRpcError {
14    fn from(e: Error) -> JsonRpcError {
15        match e {
16            Error::InvalidParams(msg, details) => JsonRpcError {
17                code: ErrorCode::InvalidParams,
18                message: msg,
19                data: Some(Value::String(details)),
20            },
21            Error::InternalError(msg) => JsonRpcError {
22                code: ErrorCode::InternalError,
23                message: msg,
24                data: None,
25            },
26        }
27    }
28}
29
30impl From<Error> for ErrorObjectOwned {
31    fn from(e: Error) -> ErrorObjectOwned {
32        let err: JsonRpcError = e.into();
33        ErrorObjectOwned::owned(err.code.code() as i32, err.message, err.data)
34    }
35}