use std::path::PathBuf;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("invalid instance id `{id}`: {reason}")]
InvalidInstanceId { id: String, reason: &'static str },
#[error("invalid relative path `{path}`: {reason}")]
InvalidRelativePath { path: String, reason: &'static str },
#[error("invalid configuration: {0}")]
InvalidConfig(String),
#[error("unsupported {kind} format version {version}")]
UnsupportedFormat { kind: &'static str, version: u32 },
#[error("{kind} `{id}` already exists")]
AlreadyExists { kind: &'static str, id: String },
#[error("{kind} `{id}` was not found")]
NotFound { kind: &'static str, id: String },
#[error("unsafe filesystem entry at `{path}`: {reason}")]
UnsafeFilesystemEntry { path: PathBuf, reason: &'static str },
#[error("provider `{provider}` failed: {message}")]
Provider { provider: String, message: String },
#[error(transparent)]
StaticProvider(#[from] crate::providers::ProviderError),
#[error(transparent)]
Trust(#[from] crate::trust::TrustError),
#[error("authentication provider `{provider}` failed: {message}")]
Authentication { provider: String, message: String },
#[error(transparent)]
Auth(#[from] AuthError),
#[error("loader `{loader}` failed: {message}")]
Loader { loader: String, message: String },
#[error(transparent)]
LoaderPlan(#[from] crate::loaders::LoaderError),
#[error("Java runtime error: {0}")]
Java(String),
#[error(transparent)]
Download(#[from] crate::download::DownloadError),
#[error(transparent)]
Minecraft(#[from] crate::minecraft::MinecraftError),
#[error("process error: {0}")]
Process(String),
#[error(transparent)]
ProcessRecovery(#[from] crate::process::ProcessRecoveryError),
#[error("coordination lock failed")]
Lock {
#[source]
source: Box<dyn std::error::Error + Send + Sync>,
},
#[error(transparent)]
Cache(#[from] crate::cache::CacheError),
#[error(transparent)]
Repair(#[from] crate::minecraft::RepairError),
#[error(transparent)]
Recovery(#[from] crate::minecraft::RecoveryError),
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
}
impl From<crate::lock::LockError> for Error {
fn from(source: crate::lock::LockError) -> Self {
Self::Lock {
source: Box::new(source),
}
}
}
impl Error {
pub(crate) fn is_lock_busy(&self) -> bool {
matches!(
self,
Self::Lock { source }
if matches!(
source.downcast_ref::<crate::lock::LockError>(),
Some(crate::lock::LockError::Busy { .. })
)
)
}
}
#[derive(thiserror::Error)]
#[non_exhaustive]
pub enum AuthError {
#[error("authentication provider `{provider}` rejected the credentials")]
InvalidCredentials { provider: String },
#[error("authentication provider `{provider}` requires two-factor authentication")]
TwoFactorRequired { provider: String },
#[error("authentication flow for `{provider}` was cancelled")]
Cancelled { provider: String },
#[error("authentication session or flow for `{provider}` expired")]
Expired { provider: String },
#[error("authentication provider `{provider}` is unavailable")]
ProviderUnavailable { provider: String },
#[error("authentication provider `{provider}` rate limited the request")]
RateLimited {
provider: String,
retry_after_seconds: Option<u64>,
},
#[error("account at authentication provider `{provider}` is restricted ({reason})")]
AccountRestricted {
provider: String,
reason: AccountRestriction,
},
#[error("account at authentication provider `{provider}` does not own Minecraft")]
NoMinecraftOwnership { provider: String },
#[error("authentication provider `{provider}` returned an invalid Minecraft profile")]
InvalidMinecraftProfile { provider: String },
#[error("credential store operation `{operation}` failed")]
CredentialStore { operation: &'static str },
#[error("authentication protocol `{provider}` rejected a response")]
Protocol { provider: String, code: String },
}
impl std::fmt::Debug for AuthError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut debug = formatter.debug_struct("AuthError");
debug.field("kind", &self.kind());
match self {
Self::InvalidCredentials { provider }
| Self::TwoFactorRequired { provider }
| Self::Cancelled { provider }
| Self::Expired { provider }
| Self::ProviderUnavailable { provider }
| Self::NoMinecraftOwnership { provider }
| Self::InvalidMinecraftProfile { provider }
| Self::Protocol { provider, .. } => {
debug.field("provider", provider);
}
Self::RateLimited {
provider,
retry_after_seconds,
} => {
debug
.field("provider", provider)
.field("retry_after_seconds", retry_after_seconds);
}
Self::AccountRestricted { provider, reason } => {
debug.field("provider", provider).field("reason", reason);
}
Self::CredentialStore { operation } => {
debug.field("operation", operation);
}
}
debug.finish()
}
}
impl AuthError {
#[must_use]
pub fn kind(&self) -> &'static str {
match self {
Self::InvalidCredentials { .. } => "invalid_credentials",
Self::TwoFactorRequired { .. } => "two_factor_required",
Self::Cancelled { .. } => "cancelled",
Self::Expired { .. } => "expired",
Self::ProviderUnavailable { .. } => "provider_unavailable",
Self::RateLimited { .. } => "rate_limited",
Self::AccountRestricted { .. } => "account_restricted",
Self::NoMinecraftOwnership { .. } => "no_minecraft_ownership",
Self::InvalidMinecraftProfile { .. } => "invalid_minecraft_profile",
Self::CredentialStore { .. } => "credential_store",
Self::Protocol { .. } => "protocol",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum AccountRestriction {
Banned,
Disabled,
ChildAccount,
RegionRestricted,
Unknown,
}
impl std::fmt::Display for AccountRestriction {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(match self {
Self::Banned => "banned",
Self::Disabled => "disabled",
Self::ChildAccount => "child_account",
Self::RegionRestricted => "region_restricted",
Self::Unknown => "unknown",
})
}
}