use crate::system::helpers::Health;
use jsonrpsee::types::{
error::{ErrorCode, ErrorObject},
ErrorObjectOwned,
};
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Node is not fully functional: {}", .0)]
NotHealthy(Health),
#[error("{0}")]
MalformattedPeerArg(String),
#[error(transparent)]
UnsafeRpcCalled(#[from] crate::policy::UnsafeRpcError),
#[error("{0}")]
Internal(String),
}
const BASE_ERROR: i32 = crate::error::base::SYSTEM;
const NOT_HEALTHY_ERROR: i32 = BASE_ERROR + 1;
const MALFORMATTED_PEER_ARG_ERROR: i32 = BASE_ERROR + 2;
impl From<Error> for ErrorObjectOwned {
fn from(e: Error) -> ErrorObjectOwned {
match e {
Error::NotHealthy(ref h) => {
ErrorObject::owned(NOT_HEALTHY_ERROR, e.to_string(), Some(h))
},
Error::MalformattedPeerArg(e) => {
ErrorObject::owned(MALFORMATTED_PEER_ARG_ERROR, e, None::<()>)
},
Error::UnsafeRpcCalled(e) => e.into(),
Error::Internal(e) => {
ErrorObjectOwned::owned(ErrorCode::InternalError.code(), e, None::<()>)
},
}
}
}