use crate::api::v4::models::AggregatedItemError;
use reqwest::Error as ReqwestError;
use std::collections::HashMap;
use std::io;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
#[error("HTTP request error: {0}")]
Http(#[from] ReqwestError),
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[error("IO error: {0}")]
Io(#[from] io::Error),
#[error("API error: {message} (code: {code})")]
Api { code: i32, message: String },
#[error("API error: {message} (code: {code})")]
ApiWithData {
code: i32,
message: String,
data: serde_json::Value,
},
#[error("Batch operation partially failed: {} of the submitted item(s) failed", errors.len())]
Aggregate {
code: i32,
message: String,
errors: HashMap<String, AggregatedItemError>,
},
#[error("Authentication error: {0}")]
Auth(String),
#[error("Invalid response: {0}")]
InvalidResponse(String),
#[error("Invalid timestamp: {0}")]
InvalidTimestamp(String),
#[error("Feature '{0}' not supported in API {1}")]
UnsupportedFeature(String, String),
#[error("Two-factor authentication required (session ID: {0})")]
TwoFactorRequired(String),
#[error("Unauthorized: {0}")]
Unauthorized(String),
#[error("CAPTCHA required for login")]
CaptchaRequired,
#[error("CAPTCHA validation failed: {0}")]
CaptchaInvalid(String),
}
impl Error {
pub fn message(&self) -> Option<&str> {
match self {
Error::Api { message, .. }
| Error::ApiWithData { message, .. }
| Error::Aggregate { message, .. } => Some(message),
_ => None,
}
}
pub fn code(&self) -> Option<i32> {
match self {
Error::Api { code, .. }
| Error::ApiWithData { code, .. }
| Error::Aggregate { code, .. } => Some(*code),
_ => None,
}
}
pub fn data(&self) -> Option<&serde_json::Value> {
match self {
Error::ApiWithData { data, .. } => Some(data),
_ => None,
}
}
pub fn aggregated_errors(&self) -> Option<&HashMap<String, AggregatedItemError>> {
match self {
Error::Aggregate { errors, .. } => Some(errors),
_ => None,
}
}
}