use std::num::ParseIntError;
use thiserror::Error;
type HeaderName = &'static str;
#[derive(Debug, Error)]
pub enum ApiError {
#[error("Header {0} is missing")]
MissingHeader(HeaderName),
#[error("Header {0} is invalid")]
InvalidHeader(HeaderName),
#[error("request error: {0}")]
RequestError(#[from] Box<ureq::Error>),
#[error("Cannot parse int")]
ParseIntError(#[from] ParseIntError),
#[error("I/O error {0}")]
IoError(#[from] std::io::Error),
#[error("Too many retries: {0}")]
TooManyRetries(Box<ApiError>),
#[error("Invalid part file - corrupted file")]
InvalidResume,
#[error("YAML parse error: {0}")]
YamlParseError(#[from] Box<serde_yaml::Error>),
}
impl ApiError {
pub fn status_code(&self) -> Option<u16> {
match self {
ApiError::RequestError(e) => match e.as_ref() {
ureq::Error::StatusCode(code) => Some(*code),
_ => None,
},
_ => None,
}
}
}
#[derive(Debug, Error)]
pub enum CacheError {
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("failed to parse token file: {0}")]
TomlDe(#[from] toml::de::Error),
#[error("failed to serialize token: {0}")]
TomlSer(#[from] toml::ser::Error),
#[error("request error: {0}")]
Request(#[from] Box<ureq::Error>),
#[error("api error: {0}")]
Api(#[from] ApiError),
#[error("authorization pending")]
AuthorizationPending,
#[error("login failed: device code was not authorized")]
LoginFailed,
}