use std::time::Duration;
#[cfg(any(feature = "async", feature = "sync"))]
use serde::Deserialize;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("blooio api error (status {status}{}): {message}", code.as_deref().map(|c| format!(", code {c}")).unwrap_or_default())]
Api {
status: u16,
code: Option<String>,
message: String,
error: Option<String>,
retry_after: Option<Duration>,
},
#[error("transport error: {0}")]
Transport(String),
#[error("failed to encode request body: {0}")]
Encode(String),
#[error("failed to decode response body: {0}")]
Decode(String),
#[cfg(feature = "webhooks")]
#[error("webhook verification failed: {0}")]
Webhook(#[from] crate::webhook::VerifyError),
}
impl Error {
#[cfg(any(feature = "async", feature = "sync"))]
pub(crate) fn transport(e: impl std::fmt::Display) -> Self {
Error::Transport(e.to_string())
}
#[cfg(any(feature = "async", feature = "sync"))]
pub(crate) fn encode(e: impl std::fmt::Display) -> Self {
Error::Encode(e.to_string())
}
pub(crate) fn decode(e: impl std::fmt::Display) -> Self {
Error::Decode(e.to_string())
}
pub fn code(&self) -> Option<&str> {
match self {
Error::Api { code, .. } => code.as_deref(),
_ => None,
}
}
pub fn status(&self) -> Option<u16> {
match self {
Error::Api { status, .. } => Some(*status),
_ => None,
}
}
pub fn retry_after(&self) -> Option<Duration> {
match self {
Error::Api { retry_after, .. } => *retry_after,
_ => None,
}
}
pub fn is_retryable(&self) -> bool {
match self {
Error::Transport(_) => true,
Error::Api { status, .. } => {
matches!(status, 408 | 425 | 429) || (500..600).contains(status)
}
_ => false,
}
}
}
#[cfg(any(feature = "async", feature = "sync"))]
#[derive(Debug, Default, Deserialize)]
pub(crate) struct ApiErrorBody {
pub error: Option<String>,
pub message: Option<String>,
#[allow(dead_code)]
pub status: Option<u16>,
pub code: Option<String>,
}