use thiserror::Error;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum Error {
#[error("request rejected: {0}")]
Request(String),
#[error("invalid thread snapshot: {0}")]
Snapshot(String),
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[error(transparent)]
Core(crate::Error),
#[error("SSE decode error: {0}")]
Decode(String),
#[error("protocol violation: {0}")]
Protocol(String),
#[error("{target} patch failed: {message}")]
Patch {
target: String,
message: String,
},
#[error("state does not match the expected type: {0}")]
State(#[source] serde_json::Error),
#[error("run failed: {message}")]
Run {
message: String,
code: Option<String>,
},
#[error("HTTP {status}: {body}")]
Http {
status: u16,
body: String,
},
#[error("transport error: {0}")]
Transport(#[source] Box<dyn std::error::Error + Send + Sync>),
#[error("invalid client configuration: {0}")]
Config(String),
}
impl From<crate::Error> for Error {
fn from(error: crate::Error) -> Self {
match error {
crate::Error::Protocol(message) => Self::Protocol(message),
other => Self::Core(other),
}
}
}
impl Error {
pub fn transport(error: impl std::error::Error + Send + Sync + 'static) -> Self {
Self::Transport(Box::new(error))
}
pub(crate) fn protocol(message: impl Into<String>) -> Self {
Self::Protocol(message.into())
}
pub(crate) fn decode(message: impl Into<String>) -> Self {
Self::Decode(message.into())
}
}
pub type Result<T, E = Error> = core::result::Result<T, E>;