1use tor_error::HasKind;
4
5use dyn_clone::DynClone;
6use tor_persist::slug::BadSlug;
7
8use std::error::Error as StdError;
9use std::fmt;
10use std::ops::Deref;
11use std::sync::Arc;
12
13use crate::raw::RawKeystoreEntry;
14use crate::{KeyPathError, KeystoreId};
15
16#[derive(thiserror::Error, Debug, Clone)]
18#[non_exhaustive]
19pub enum Error {
20 #[error("{0}")]
22 Corruption(#[from] KeystoreCorruptionError),
23
24 #[error("{0}")]
26 Keystore(#[from] Arc<dyn KeystoreError>),
27
28 #[error("Key already exists")]
37 KeyAlreadyExists,
38
39 #[error("{0}")]
41 KeyForge(#[from] tor_key_forge::Error),
42
43 #[error("{0}")]
45 InvalidCert(#[from] tor_key_forge::InvalidCertError),
46
47 #[error("Keystore {0} not found")]
51 KeystoreNotFound(KeystoreId),
52
53 #[error("Internal error")]
55 Bug(#[from] tor_error::Bug),
56}
57
58pub trait KeystoreError:
60 HasKind + StdError + DynClone + fmt::Debug + fmt::Display + Send + Sync + 'static
61{
62}
63
64impl HasKind for Error {
65 fn kind(&self) -> tor_error::ErrorKind {
66 use tor_error::ErrorKind as EK;
67 use Error as E;
68
69 match self {
70 E::Keystore(e) => e.kind(),
71 E::Corruption(_) => EK::KeystoreCorrupted,
72 E::KeyAlreadyExists => EK::BadApiUsage, E::KeystoreNotFound(_) => EK::BadApiUsage, E::KeyForge(_) => EK::BadApiUsage,
75 E::InvalidCert(_) => EK::BadApiUsage, E::Bug(e) => e.kind(),
77 }
78 }
79}
80
81#[derive(thiserror::Error, Debug, Clone)]
89#[error("Invalid ArtiPath")]
90#[non_exhaustive]
91pub enum ArtiPathSyntaxError {
92 #[error("{0}")]
94 Slug(#[from] BadSlug),
95
96 #[error("Internal error")]
98 Bug(#[from] tor_error::Bug),
99}
100
101#[derive(thiserror::Error, Debug, Clone)]
103#[error("Keystore corruption")]
104#[non_exhaustive]
105pub enum KeystoreCorruptionError {
106 #[error("{0}")]
108 KeyPath(#[from] KeyPathError),
109
110 #[error("Missing certificate for key")]
112 MissingCertificate,
113
114 #[error("Subject key of certificate not found")]
116 MissingSubjectKey,
117
118 #[error("Missing signing key for certificate")]
120 MissingSigningKey,
121}
122
123#[derive(thiserror::Error, PartialEq, Eq, Debug, Clone)]
125#[error("unknown key type: arti_extension={arti_extension}")]
126pub struct UnknownKeyTypeError {
127 pub(crate) arti_extension: String,
129}
130
131#[derive(Clone, Debug, amplify::Getters, thiserror::Error)]
133#[error("Unrecognized keystore entry")]
134pub struct UnrecognizedEntryError {
135 entry: UnrecognizedEntry,
137 #[source]
150 error: Arc<dyn KeystoreError>,
151}
152
153impl UnrecognizedEntryError {
154 pub(crate) fn new(entry: UnrecognizedEntry, error: Arc<dyn KeystoreError>) -> Self {
157 Self { entry, error }
158 }
159}
160
161#[derive(Debug, Clone, PartialEq, derive_more::From, derive_more::Into)]
163pub struct UnrecognizedEntry(RawKeystoreEntry);
164
165#[cfg(feature = "onion-service-cli-extra")]
166impl Deref for UnrecognizedEntry {
167 type Target = RawKeystoreEntry;
168 fn deref(&self) -> &Self::Target {
169 &self.0
170 }
171}
172
173#[cfg(test)]
174mod tests {
175 #![allow(clippy::bool_assert_comparison)]
177 #![allow(clippy::clone_on_copy)]
178 #![allow(clippy::dbg_macro)]
179 #![allow(clippy::mixed_attributes_style)]
180 #![allow(clippy::print_stderr)]
181 #![allow(clippy::print_stdout)]
182 #![allow(clippy::single_char_pattern)]
183 #![allow(clippy::unwrap_used)]
184 #![allow(clippy::unchecked_duration_subtraction)]
185 #![allow(clippy::useless_vec)]
186 #![allow(clippy::needless_pass_by_value)]
187 use super::*;
189 use tor_error::ErrorKind;
190
191 #[derive(Debug, Copy, Clone, PartialEq, thiserror::Error)]
192 #[error("The source of a test error")]
193 struct TestErrorSource;
194
195 #[derive(Debug, Clone, thiserror::Error)]
196 #[error("A test error")]
197 struct TestError(#[from] TestErrorSource);
198
199 impl KeystoreError for TestError {}
200
201 impl HasKind for TestError {
202 fn kind(&self) -> ErrorKind {
203 ErrorKind::Other
204 }
205 }
206
207 #[test]
208 fn error_source() {
209 let e: Error = (Arc::new(TestError(TestErrorSource)) as Arc<dyn KeystoreError>).into();
210
211 assert_eq!(
212 e.source().unwrap().to_string(),
213 TestError(TestErrorSource).to_string()
214 );
215 }
216}