use agent_client_protocol::Error as RpcError;
use oino_client::ClientError;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum AcpError {
#[error("Session not found: {0}")]
SessionNotFound(String),
#[error("{0}")]
InvalidParams(String),
#[error("{0}")]
Factory(String),
#[error("{0}")]
Command(String),
#[error("client request {method} failed: {message}")]
Client {
method: &'static str,
message: String,
},
#[error("connection closed")]
Closed,
#[error("protocol: {0}")]
Protocol(String),
}
impl From<ClientError> for AcpError {
fn from(error: ClientError) -> Self {
match error {
ClientError::Command(message) => Self::Command(message),
ClientError::Closed => Self::Closed,
other => Self::Protocol(other.to_string()),
}
}
}
impl From<serde_json::Error> for AcpError {
fn from(error: serde_json::Error) -> Self {
Self::Protocol(error.to_string())
}
}
impl From<AcpError> for RpcError {
fn from(error: AcpError) -> Self {
let code = match &error {
AcpError::SessionNotFound(_) => RpcError::resource_not_found(None).code,
AcpError::InvalidParams(_) => RpcError::invalid_params().code,
_ => RpcError::internal_error().code,
};
RpcError::new(code.into(), error.to_string())
}
}