use snafu::Snafu;
pub mod code {
pub const PARSE_ERROR: i32 = -32700;
pub const INVALID_REQUEST: i32 = -32600;
pub const METHOD_NOT_FOUND: i32 = -32601;
pub const INVALID_PARAMS: i32 = -32602;
pub const INTERNAL_ERROR: i32 = -32603;
pub const SERVER_ERROR: i32 = -32000;
pub const ENGINE_ERROR: i32 = -32001;
}
#[derive(Debug, Snafu)]
#[snafu(visibility(pub))]
pub enum RpcError {
#[snafu(display("Method not found: {method}"))]
MethodNotFound { method: String },
#[snafu(display("Invalid params: {detail}"))]
InvalidParams { detail: String },
#[snafu(display("Engine error: {message}"))]
Engine { message: String },
#[snafu(display("Internal error: {detail}"))]
Internal { detail: String },
}
impl RpcError {
pub fn code(&self) -> i32 {
match self {
RpcError::MethodNotFound { .. } => code::METHOD_NOT_FOUND,
RpcError::InvalidParams { .. } => code::INVALID_PARAMS,
RpcError::Engine { .. } => code::ENGINE_ERROR,
RpcError::Internal { .. } => code::INTERNAL_ERROR,
}
}
}
#[derive(Debug, Snafu)]
pub enum DaemonError {
#[snafu(display("I/O error: {source}"))]
Io { source: std::io::Error },
#[snafu(display("Failed to serialize response: {source}"))]
Serialize { source: serde_json::Error },
#[snafu(display("Failed to bind listener: {source}"))]
Bind { source: std::io::Error },
#[snafu(display("Shutdown signaled"))]
Shutdown,
}
pub type Result<T> = std::result::Result<T, DaemonError>;
impl From<std::io::Error> for DaemonError {
fn from(source: std::io::Error) -> Self {
DaemonError::Io { source }
}
}
impl From<serde_json::Error> for DaemonError {
fn from(source: serde_json::Error) -> Self {
DaemonError::Serialize { source }
}
}