1#[derive(Clone, PartialEq, Eq, Debug)]
3pub enum CryptoError {
4 UnsupportedVersion(u8),
7 OldVersion(u8),
9 DecryptFailed,
11 BadLength {
13 step: &'static str,
14 actual: usize,
15 expected: usize,
16 },
17 BadKey,
19 BadFormat(&'static str),
22 ObjectMismatch(&'static str),
27 SignatureFailed,
29 NotSupportedByVault,
31}
32
33impl CryptoError {
34 pub fn serde_err(&self) -> String {
35 match *self {
36 CryptoError::UnsupportedVersion(version) => {
37 format!("crypto version ({}) not supported.", version)
38 }
39 CryptoError::OldVersion(version) => {
40 format!("crypto version ({}) old and deemed unsafe", version)
41 }
42 CryptoError::DecryptFailed => "could not decrypt with key".to_string(),
43 CryptoError::BadLength {
44 step,
45 actual,
46 expected,
47 } => format!(
48 "expected data length {}, but got {} on step [{}]",
49 expected, actual, step
50 ),
51 CryptoError::BadKey => "crypto key is weak or invalid".to_string(),
52 CryptoError::BadFormat(s) => format!("format of data does not match spec: {}", s),
53 CryptoError::ObjectMismatch(s) => format!("object mismatch: {}", s),
54 CryptoError::SignatureFailed => "signature verification failed".to_string(),
55 CryptoError::NotSupportedByVault => "vault doesn't support this operation".to_string(),
56 }
57 }
58}
59
60use std::fmt;
61impl fmt::Display for CryptoError {
62 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63 match *self {
64 CryptoError::UnsupportedVersion(version) => {
65 write!(f, "Chosen crypto version ({}) not supported.", version)
66 }
67 CryptoError::OldVersion(version) => {
68 write!(f, "Crypto version ({}) is old and deemed unsafe.", version)
69 }
70 CryptoError::DecryptFailed => write!(f, "Could not decrypt with key"),
71 CryptoError::BadLength {
72 step,
73 actual,
74 expected,
75 } => write!(
76 f,
77 "Expected data length {}, but got {} on step [{}]",
78 expected, actual, step
79 ),
80 CryptoError::BadKey => write!(f, "Crypto key is weak or invalid"),
81 CryptoError::BadFormat(s) => write!(f, "Format of data does not match spec: {}", s),
82 CryptoError::ObjectMismatch(s) => write!(f, "Object mismatch: {}", s),
83 CryptoError::SignatureFailed => write!(f, "Signature verification failed"),
84 CryptoError::NotSupportedByVault => write!(f, "Vault doesn't support this operation"),
85 }
86 }
87}
88
89impl std::error::Error for CryptoError {}