use std::fmt;
#[derive(Debug)]
pub enum ServiceError {
InvalidParams(String),
NotFound(String),
Internal(String),
SerdeError(serde_json::Error),
}
impl fmt::Display for ServiceError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ServiceError::InvalidParams(msg) => write!(f, "Invalid parameters: {}", msg),
ServiceError::NotFound(msg) => write!(f, "Not found: {}", msg),
ServiceError::Internal(msg) => write!(f, "Internal error: {}", msg),
ServiceError::SerdeError(e) => write!(f, "Serialization error: {}", e),
}
}
}
impl std::error::Error for ServiceError {}
impl From<serde_json::Error> for ServiceError {
fn from(err: serde_json::Error) -> Self {
ServiceError::SerdeError(err)
}
}
#[cfg(feature = "mcp")]
impl From<ServiceError> for rmcp::model::ErrorData {
fn from(err: ServiceError) -> Self {
match err {
ServiceError::InvalidParams(msg) => {
rmcp::model::ErrorData::invalid_params(msg, None)
}
ServiceError::NotFound(msg) => {
rmcp::model::ErrorData::resource_not_found(msg, None)
}
ServiceError::Internal(msg) => {
rmcp::model::ErrorData::internal_error(msg, None)
}
ServiceError::SerdeError(e) => {
rmcp::model::ErrorData::internal_error(format!("JSON error: {}", e), None)
}
}
}
}