use std::fmt;
#[derive(Debug)]
pub enum AcpServerError {
SessionNotFound(String),
MaxSessionsReached(usize),
ShuttingDown,
UnsupportedVersion {
requested: String,
supported: Vec<String>,
},
MalformedMessage(String),
Internal(String),
Transport(String),
Execution(String),
}
impl AcpServerError {
pub fn error_code(&self) -> &'static str {
match self {
Self::SessionNotFound(_) => "session_not_found",
Self::MaxSessionsReached(_) => "max_sessions_reached",
Self::ShuttingDown => "shutting_down",
Self::UnsupportedVersion { .. } => "unsupported_version",
Self::MalformedMessage(_) => "malformed_message",
Self::Internal(_) => "internal_error",
Self::Transport(_) => "transport_error",
Self::Execution(_) => "execution_error",
}
}
pub fn client_message(&self) -> String {
match self {
Self::SessionNotFound(id) => format!("session not found: {id}"),
Self::MaxSessionsReached(limit) => {
format!("max sessions reached (limit: {limit})")
}
Self::ShuttingDown => "server is shutting down".to_string(),
Self::UnsupportedVersion { requested, supported } => {
format!("unsupported protocol version: {requested}, supported: {supported:?}")
}
Self::MalformedMessage(msg) => format!("malformed message: {msg}"),
Self::Internal(_) => "internal server error".to_string(),
Self::Transport(msg) => format!("transport error: {msg}"),
Self::Execution(msg) => format!("execution error: {msg}"),
}
}
pub fn to_error_response(&self) -> ErrorResponse {
ErrorResponse { error_code: self.error_code().to_string(), message: self.client_message() }
}
}
impl fmt::Display for AcpServerError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::SessionNotFound(id) => write!(f, "session not found: {id}"),
Self::MaxSessionsReached(limit) => {
write!(f, "max sessions reached (limit: {limit})")
}
Self::ShuttingDown => write!(f, "server is shutting down"),
Self::UnsupportedVersion { requested, supported } => {
write!(f, "unsupported protocol version: {requested}, supported: {supported:?}")
}
Self::MalformedMessage(msg) => write!(f, "malformed message: {msg}"),
Self::Internal(msg) => write!(f, "internal server error: {msg}"),
Self::Transport(msg) => write!(f, "transport error: {msg}"),
Self::Execution(msg) => write!(f, "execution error: {msg}"),
}
}
}
impl std::error::Error for AcpServerError {}
impl From<AcpServerError> for crate::AcpError {
fn from(err: AcpServerError) -> Self {
crate::AcpError::Protocol(err.to_string())
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ErrorResponse {
pub error_code: String,
pub message: String,
}