use std::{error::Error};
use thiserror::Error;
use super::utils::ResponseError;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum BodyError {
#[error("URL encode error: {0}")]
UrlEncoded(#[from] serde_urlencoded::ser::Error),
#[error("JSON encode error: {0}")]
Json(#[from] serde_json::Error),
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ApiError<E>
where
E: Error + Send + Sync + 'static,
{
#[error("failed to create form data: {0}")]
Body(#[from] BodyError),
#[error("client error: {0}")]
Client(E),
#[error("url parse error: {0}")]
Parse(#[from] url::ParseError),
#[error("Endpoint requires authentication, but no API key was provided")]
RequiresAuthentication,
#[error("Error in the HTTP response at url [{url}]: source")]
Response {
source: ResponseError,
url: http::Uri,
},
}
impl<E> ApiError<E>
where
E: Error + Send + Sync + 'static,
{
pub fn client(source: E) -> Self {
Self::Client(source)
}
pub(crate) fn from_http_response(source: ResponseError, url: http::Uri) -> Self {
Self::Response { source, url }
}
}