use thiserror::Error;
#[cfg(feature = "http")]
#[cfg_attr(docsrs, doc(cfg(feature = "http")))]
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ApiStatusError {
pub status: u16,
pub url: String,
pub message: String,
pub body: String,
}
#[cfg(feature = "http")]
impl ApiStatusError {
#[must_use]
pub fn new(
status: u16,
url: impl Into<String>,
message: impl Into<String>,
body: impl Into<String>,
) -> Self {
Self {
status,
url: url.into(),
message: message.into(),
body: body.into(),
}
}
}
#[cfg(feature = "yggdrasil")]
#[cfg_attr(docsrs, doc(cfg(feature = "yggdrasil")))]
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct YggdrasilError {
pub error: String,
pub error_message: String,
pub cause: Option<String>,
}
#[cfg(feature = "yggdrasil")]
impl YggdrasilError {
#[must_use]
pub fn new(
error: impl Into<String>,
error_message: impl Into<String>,
cause: Option<String>,
) -> Self {
Self {
error: error.into(),
error_message: error_message.into(),
cause,
}
}
}
#[non_exhaustive]
#[derive(Debug, Error)]
pub enum Error {
#[cfg(feature = "http")]
#[cfg_attr(docsrs, doc(cfg(feature = "http")))]
#[error("HTTP request failed: {0}")]
Http(#[from] reqwest::Error),
#[error("failed to serialize or deserialize JSON: {0}")]
Json(#[from] serde_json::Error),
#[cfg(feature = "http")]
#[cfg_attr(docsrs, doc(cfg(feature = "http")))]
#[error("invalid URL: {0}")]
Url(#[from] url::ParseError),
#[cfg(feature = "http")]
#[cfg_attr(docsrs, doc(cfg(feature = "http")))]
#[error("invalid HTTP header value: {0}")]
InvalidHeader(#[from] reqwest::header::InvalidHeaderValue),
#[cfg(feature = "http")]
#[cfg_attr(docsrs, doc(cfg(feature = "http")))]
#[error("{}", format_rate_limit(*retry_after_secs))]
RateLimitExceeded {
retry_after_secs: Option<u64>,
limit: Option<u32>,
remaining: Option<u32>,
},
#[cfg(feature = "http")]
#[cfg_attr(docsrs, doc(cfg(feature = "http")))]
#[error("API returned HTTP {} for {}: {}", .0.status, .0.url, .0.message)]
ApiStatus(Box<ApiStatusError>),
#[error("invalid input: {0}")]
InvalidInput(String),
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[cfg(feature = "profile")]
#[cfg_attr(docsrs, doc(cfg(feature = "profile")))]
#[error("base64 decode error: {0}")]
Base64(#[source] Box<dyn std::error::Error + Send + Sync>),
#[cfg(feature = "loaders")]
#[cfg_attr(docsrs, doc(cfg(feature = "loaders")))]
#[error("XML parse error: {0}")]
Xml(#[source] Box<dyn std::error::Error + Send + Sync>),
#[cfg(feature = "mojang")]
#[cfg_attr(docsrs, doc(cfg(feature = "mojang")))]
#[error("XSTS rejected ({:?}, XErr={}): {}", .0.kind, .0.xerr, .0.message)]
XstsRejected(Box<crate::mojang::XstsError>),
#[cfg(feature = "mojang")]
#[cfg_attr(docsrs, doc(cfg(feature = "mojang")))]
#[error("missing Xbox user hash (uhs) in XBL/XSTS DisplayClaims")]
MissingUserHash,
#[cfg(feature = "mojang")]
#[cfg_attr(docsrs, doc(cfg(feature = "mojang")))]
#[error("Microsoft device code expired before authorization")]
DeviceCodeExpired,
#[cfg(feature = "mojang")]
#[cfg_attr(docsrs, doc(cfg(feature = "mojang")))]
#[error("Microsoft device code authorization was declined by the user")]
DeviceCodeDeclined,
#[cfg(feature = "mojang")]
#[cfg_attr(docsrs, doc(cfg(feature = "mojang")))]
#[error("Microsoft account does not own Minecraft: Java Edition")]
DoesNotOwnMinecraft,
#[cfg(any(feature = "mojang", feature = "yggdrasil"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "mojang", feature = "yggdrasil"))))]
#[error("profile has no textures property")]
MissingTexturesProperty,
#[cfg(feature = "yggdrasil")]
#[cfg_attr(docsrs, doc(cfg(feature = "yggdrasil")))]
#[error("Yggdrasil error ({}): {}", .0.error, .0.error_message)]
Yggdrasil(Box<YggdrasilError>),
#[cfg(feature = "yggdrasil")]
#[cfg_attr(docsrs, doc(cfg(feature = "yggdrasil")))]
#[error("invalid X-Authlib-Injector-API-Location header")]
InvalidAliLocation,
#[cfg(feature = "yggdrasil-verify")]
#[cfg_attr(docsrs, doc(cfg(feature = "yggdrasil-verify")))]
#[error("profile property '{0}' is missing a signature")]
MissingSignature(String),
#[cfg(feature = "yggdrasil-verify")]
#[cfg_attr(docsrs, doc(cfg(feature = "yggdrasil-verify")))]
#[error("invalid profile signature or public key: {0}")]
InvalidSignature(String),
#[cfg(feature = "blessing")]
#[cfg_attr(docsrs, doc(cfg(feature = "blessing")))]
#[error("Blessing Skin API returned code {code}: {message}")]
BlessingApi {
code: i32,
message: String,
},
}
impl Error {
#[cfg(feature = "http")]
pub(crate) fn api_status(
status: u16,
url: impl Into<String>,
message: impl Into<String>,
body: impl Into<String>,
) -> Self {
Self::ApiStatus(Box::new(ApiStatusError::new(status, url, message, body)))
}
#[cfg(feature = "profile")]
pub(crate) fn base64(err: impl std::error::Error + Send + Sync + 'static) -> Self {
Self::Base64(Box::new(err))
}
#[cfg(feature = "loaders")]
pub(crate) fn xml(err: impl std::error::Error + Send + Sync + 'static) -> Self {
Self::Xml(Box::new(err))
}
#[cfg(feature = "yggdrasil")]
pub(crate) fn yggdrasil(
error: impl Into<String>,
error_message: impl Into<String>,
cause: Option<String>,
) -> Self {
Self::Yggdrasil(Box::new(YggdrasilError::new(error, error_message, cause)))
}
#[must_use]
pub fn status_code(&self) -> Option<u16> {
match self {
#[cfg(feature = "http")]
Error::ApiStatus(err) => Some(err.status),
#[cfg(feature = "http")]
Error::RateLimitExceeded { .. } => Some(429),
_ => None,
}
}
#[cfg(feature = "http")]
#[cfg_attr(docsrs, doc(cfg(feature = "http")))]
#[must_use]
pub fn api_status_error(&self) -> Option<&ApiStatusError> {
match self {
Error::ApiStatus(err) => Some(err),
_ => None,
}
}
#[cfg(feature = "yggdrasil")]
#[cfg_attr(docsrs, doc(cfg(feature = "yggdrasil")))]
#[must_use]
pub fn yggdrasil_error(&self) -> Option<&YggdrasilError> {
match self {
Error::Yggdrasil(err) => Some(err),
_ => None,
}
}
#[cfg(feature = "mojang")]
#[cfg_attr(docsrs, doc(cfg(feature = "mojang")))]
#[must_use]
pub fn xsts_error(&self) -> Option<&crate::mojang::XstsError> {
match self {
Error::XstsRejected(err) => Some(err),
_ => None,
}
}
#[must_use]
pub fn is_not_found(&self) -> bool {
self.status_code() == Some(404)
}
#[must_use]
pub fn is_rate_limited(&self) -> bool {
self.status_code() == Some(429)
}
}
pub type Result<T> = std::result::Result<T, Error>;
#[cfg(feature = "http")]
fn format_rate_limit(retry_after_secs: Option<u64>) -> String {
match retry_after_secs {
Some(secs) => format!("rate limit exceeded, retry after {secs}s"),
None => "rate limit exceeded".to_string(),
}
}