#[cfg(feature = "migration")]
use crate::migration::MigrationError;
#[derive(Debug)]
pub enum Error {
HttpError(u16, String),
IoError(std::io::Error),
#[cfg(feature = "migration")]
MigrationError(MigrationError),
ResponseError(crate::Value),
ResultError(String),
SerdeError(serde_json::Error),
#[cfg(feature = "ureq")]
UreqError(Box<ureq::Error>, Box<Option<crate::Value>>),
#[cfg(feature = "url")]
UrlParseError(url::ParseError),
}
impl std::error::Error for Error {}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::HttpError(status, msg) => {
f.write_fmt(format_args!("HTTP Status {status}: {msg}"))
}
Error::IoError(inner) => inner.fmt(f),
#[cfg(feature = "migration")]
Error::MigrationError(inner) => inner.fmt(f),
Error::ResponseError(v) => f.write_fmt(format_args!("response error: {v:?}")),
Error::ResultError(msg) => f.write_str(msg),
Error::SerdeError(inner) => inner.fmt(f),
#[cfg(feature = "ureq")]
Error::UreqError(inner, sql) => f.write_fmt(format_args!("{inner} [{sql:?}]")),
#[cfg(feature = "url")]
Error::UrlParseError(inner) => inner.fmt(f),
}
}
}
impl From<&str> for Error {
fn from(err: &str) -> Self {
Self::ResultError(err.to_string())
}
}
impl From<std::io::Error> for Error {
fn from(value: std::io::Error) -> Self {
Self::IoError(value)
}
}
#[cfg(feature = "migration")]
impl From<MigrationError> for Error {
fn from(value: MigrationError) -> Self {
Self::MigrationError(value)
}
}
impl From<serde_json::Error> for Error {
fn from(value: serde_json::Error) -> Self {
Self::SerdeError(value)
}
}
#[cfg(feature = "ureq")]
impl From<ureq::Error> for Error {
fn from(value: ureq::Error) -> Self {
Self::UreqError(Box::new(value), Box::new(None))
}
}
#[cfg(feature = "url")]
impl From<url::ParseError> for Error {
fn from(value: url::ParseError) -> Self {
Self::UrlParseError(value)
}
}