use super::Severity;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum IdentityError {
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("Identity file is corrupted: {0}")]
CorruptedIdentityFile(String),
#[error("Identity not found: {0}")]
IdentityNotFound(String),
#[error("Invalid seed phrase: {0}")]
InvalidSeedPhrase(String),
#[error("Failed to decrypt identity file: {0}")]
DecryptionFailed(String),
}
impl PartialEq for IdentityError {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Io(a), Self::Io(b)) => a.kind() == b.kind(),
(Self::CorruptedIdentityFile(a), Self::CorruptedIdentityFile(b)) => a == b,
(Self::IdentityNotFound(a), Self::IdentityNotFound(b)) => a == b,
(Self::InvalidSeedPhrase(a), Self::InvalidSeedPhrase(b)) => a == b,
(Self::DecryptionFailed(a), Self::DecryptionFailed(b)) => a == b,
_ => false,
}
}
}
impl Eq for IdentityError {}
impl IdentityError {
pub fn code(&self) -> &'static str {
match self {
Self::Io(_) => "KIN-IDN-001",
Self::CorruptedIdentityFile(_) => "KIN-IDN-002",
Self::IdentityNotFound(_) => "KIN-IDN-003",
Self::InvalidSeedPhrase(_) => "KIN-IDN-004",
Self::DecryptionFailed(_) => "KIN-IDN-005",
}
}
pub fn error_type_uri(&self) -> String {
format!("{}/errors/{}", crate::constants::DOCS_URL, self.code())
}
pub fn severity(&self) -> Severity {
match self {
Self::Io(_)
| Self::CorruptedIdentityFile(_)
| Self::IdentityNotFound(_)
| Self::DecryptionFailed(_) => Severity::Error,
Self::InvalidSeedPhrase(_) => Severity::Warning,
}
}
pub fn is_retryable(&self) -> bool {
false
}
pub fn user_message(&self) -> String {
match self {
Self::Io(_) => {
"An I/O error occurred while reading or writing the identity file.".to_string()
}
Self::CorruptedIdentityFile(_) => {
"The identity file is corrupted and cannot be used.".to_string()
}
Self::IdentityNotFound(_) => "The identity file could not be found.".to_string(),
Self::InvalidSeedPhrase(_) => "The provided seed phrase is invalid.".to_string(),
Self::DecryptionFailed(_) => {
"Failed to decrypt the identity file. Incorrect password or corrupted payload."
.to_string()
}
}
}
}