use ironflow_types::ErrorEnvelope;
use serde::Deserialize;
use thiserror::Error;
#[derive(Debug, Clone, Deserialize)]
pub(crate) struct ApiErrorEnvelope {
pub error: ErrorEnvelope,
}
#[derive(Debug, Error)]
pub enum Error {
#[error("HTTP error: {0}")]
Http(#[from] reqwest::Error),
#[error("API error {status}: [{code}] {message}")]
Api {
status: u16,
code: String,
message: String,
},
#[error("deserialization error: {0}")]
Deserialize(String),
#[error("SSE error: {0}")]
Sse(String),
}
impl Error {
pub fn api(status: u16, code: impl Into<String>, message: impl Into<String>) -> Self {
Self::Api {
status,
code: code.into(),
message: message.into(),
}
}
pub fn is_api_error(&self) -> bool {
matches!(self, Self::Api { .. })
}
pub fn status(&self) -> Option<u16> {
match self {
Self::Api { status, .. } => Some(*status),
_ => None,
}
}
pub fn code(&self) -> Option<&str> {
match self {
Self::Api { code, .. } => Some(code),
_ => None,
}
}
}