use tor_error::HasKind;
use dyn_clone::DynClone;
use tor_persist::slug::BadSlug;
use std::error::Error as StdError;
use std::fmt;
use std::sync::Arc;
use crate::KeyPathError;
#[derive(thiserror::Error, Debug, Clone)]
#[non_exhaustive]
pub enum Error {
#[error("{0}")]
Corruption(#[from] KeystoreCorruptionError),
#[error("{0}")]
Keystore(#[from] Arc<dyn KeystoreError>),
#[error("Key already exists")]
KeyAlreadyExists,
#[error("{0}")]
KeyForge(#[from] tor_key_forge::Error),
#[error("Internal error")]
Bug(#[from] tor_error::Bug),
}
pub trait KeystoreError:
HasKind + StdError + DynClone + fmt::Debug + fmt::Display + Send + Sync + 'static
{
}
impl HasKind for Error {
fn kind(&self) -> tor_error::ErrorKind {
use tor_error::ErrorKind as EK;
use Error as E;
match self {
E::Keystore(e) => e.kind(),
E::Corruption(_) => EK::KeystoreCorrupted,
E::KeyAlreadyExists => EK::BadApiUsage, E::KeyForge(_) => EK::BadApiUsage,
E::Bug(e) => e.kind(),
}
}
}
#[derive(thiserror::Error, Debug, Clone, Eq, PartialEq)]
#[error("Invalid ArtiPath")]
#[non_exhaustive]
pub enum ArtiPathSyntaxError {
#[error("{0}")]
Slug(#[from] BadSlug),
}
#[derive(thiserror::Error, Debug, Clone)]
#[error("Keystore corruption")]
#[non_exhaustive]
pub enum KeystoreCorruptionError {
#[error("{0}")]
KeyPath(#[from] KeyPathError),
}
#[derive(thiserror::Error, PartialEq, Eq, Debug, Clone)]
#[error("unknown key type: arti_extension={arti_extension}")]
pub struct UnknownKeyTypeError {
pub(crate) arti_extension: String,
}
#[cfg(test)]
mod tests {
#![allow(clippy::bool_assert_comparison)]
#![allow(clippy::clone_on_copy)]
#![allow(clippy::dbg_macro)]
#![allow(clippy::mixed_attributes_style)]
#![allow(clippy::print_stderr)]
#![allow(clippy::print_stdout)]
#![allow(clippy::single_char_pattern)]
#![allow(clippy::unwrap_used)]
#![allow(clippy::unchecked_duration_subtraction)]
#![allow(clippy::useless_vec)]
#![allow(clippy::needless_pass_by_value)]
use super::*;
use tor_error::ErrorKind;
#[derive(Debug, Copy, Clone, PartialEq, thiserror::Error)]
#[error("The source of a test error")]
struct TestErrorSource;
#[derive(Debug, Clone, thiserror::Error)]
#[error("A test error")]
struct TestError(#[from] TestErrorSource);
impl KeystoreError for TestError {}
impl HasKind for TestError {
fn kind(&self) -> ErrorKind {
ErrorKind::Other
}
}
#[test]
fn error_source() {
let e: Error = (Arc::new(TestError(TestErrorSource)) as Arc<dyn KeystoreError>).into();
assert_eq!(
e.source().unwrap().to_string(),
TestError(TestErrorSource).to_string()
);
}
}