use displaydoc::Display;
use jsonrpsee::types::{ErrorObject, ErrorObjectOwned};
use massa_hash::MassaHashError;
use massa_models::error::ModelsError;
use massa_time::TimeError;
use massa_versioning::versioning_factory::FactoryError;
use massa_wallet::WalletError;
#[non_exhaustive]
#[derive(Display, thiserror::Error, Debug)]
pub enum ApiError {
SendChannelError(String),
ReceiveChannelError(String),
MassaHashError(#[from] MassaHashError),
ConsensusError(String),
ExecutionError(String),
ProtocolError(String),
ModelsError(#[from] ModelsError),
TimeError(#[from] TimeError),
WalletError(#[from] WalletError),
NotFound,
InconsistencyError(String),
MissingCommandSender(String),
MissingConfig(String),
WrongAPI,
BadRequest(String),
InternalServerError(String),
FactoryError(#[from] FactoryError),
}
impl From<ApiError> for ErrorObjectOwned {
fn from(err: ApiError) -> Self {
let code = match err {
ApiError::BadRequest(_) => -32000,
ApiError::InternalServerError(_) => -32001,
ApiError::NotFound => -32004,
ApiError::SendChannelError(_) => -32006,
ApiError::ReceiveChannelError(_) => -32007,
ApiError::MassaHashError(_) => -32008,
ApiError::ConsensusError(_) => -32009,
ApiError::ExecutionError(_) => -32010,
ApiError::ProtocolError(_) => -32012,
ApiError::ModelsError(_) => -32013,
ApiError::TimeError(_) => -32014,
ApiError::WalletError(_) => -32015,
ApiError::InconsistencyError(_) => -32016,
ApiError::MissingCommandSender(_) => -32017,
ApiError::MissingConfig(_) => -32018,
ApiError::WrongAPI => -32019,
ApiError::FactoryError(_) => -32020,
};
ErrorObject::owned(code, err.to_string(), None::<()>)
}
}