use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Error, Debug)]
pub enum Error {
#[error("HTTP request failed: {0}")]
Http(#[from] reqwest::Error),
#[error("WebSocket error: {0}")]
WebSocket(#[source] Box<tokio_tungstenite::tungstenite::Error>),
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[error("API error ({status}): {message}")]
Api {
status: u16,
message: String,
},
#[error("Sprite not found: {0}")]
NotFound(String),
#[error("Invalid URL: {0}")]
InvalidUrl(#[from] url::ParseError),
#[error("Command failed with exit code {code}: {message}")]
ExecFailed {
code: i32,
message: String,
},
#[error("Operation timed out")]
Timeout,
#[error("Invalid response: {0}")]
InvalidResponse(String),
#[error("Connection error: {0}")]
Connection(String),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
}
impl From<tokio_tungstenite::tungstenite::Error> for Error {
fn from(err: tokio_tungstenite::tungstenite::Error) -> Self {
Self::WebSocket(Box::new(err))
}
}
impl Error {
pub fn connection(message: impl Into<String>) -> Self {
Self::Connection(message.into())
}
pub fn api(status: u16, message: impl Into<String>) -> Self {
Self::Api {
status,
message: message.into(),
}
}
pub fn not_found(name: impl Into<String>) -> Self {
Self::NotFound(name.into())
}
pub fn exec_failed(code: i32, message: impl Into<String>) -> Self {
Self::ExecFailed {
code,
message: message.into(),
}
}
}