use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use std::time::SystemTimeError;
use reqwest::header::InvalidHeaderValue;
#[derive(Debug, Deserialize, Serialize)]
pub struct ApiErrorDetail {
pub message: String,
pub code: i32,
pub field: Option<String>, }
#[derive(Debug, Deserialize, Serialize)]
pub struct ApiErrorBody {
pub errors: Vec<i32>,
pub messages: Vec<String>,
}
#[derive(Error, Debug)]
pub enum Error {
#[error("HTTP request failed: {0}")]
Reqwest(#[from] reqwest::Error),
#[error("URL parsing error: {0}")]
Url(#[from] url::ParseError),
#[error("URL encoding error: {0}")]
UrlEncoded(#[from] serde_urlencoded::ser::Error),
#[error("JSON serialization/deserialization error: {0}")]
Json(#[from] serde_json::Error),
#[error("HMAC key initialization error: {0}")]
HmacKey(#[from] hmac::digest::InvalidLength),
#[error("System time error: {0}")]
SystemTime(#[from] SystemTimeError),
#[error("Invalid HTTP header value: {0}")]
InvalidHeaderValue(#[from] InvalidHeaderValue),
#[error("API method '{0}' not found in method settings")]
MethodNotFound(&'static str),
#[error("Missing required path parameter: {0}")]
MissingPathParameter(&'static str),
#[error("API request failed with status {status}: {body}")]
Api {
status: StatusCode,
body: String, api_error_details: Option<ApiErrorBody>, },
#[error("An unknown error occurred: {0}")]
Other(String),
}
impl Error {
pub(crate) fn api_error(status: StatusCode, body: String) -> Self {
let api_error_details: Option<ApiErrorBody> = serde_json::from_str(&body).ok();
Error::Api {
status,
body,
api_error_details,
}
}
}