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>,
},
#[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 {
pub(crate) fn transport(e: impl std::fmt::Display) -> Self {
Error::Transport(e.to_string())
}
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,
}
}
}
#[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>,
}