crypto/
error.rs

1// Copyright 2020 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use core::fmt::{Display, Formatter, Result as FmtResult};
5
6pub type Result<T, E = Error> = core::result::Result<T, E>;
7
8/// Error type of crypto.rs
9#[non_exhaustive]
10#[derive(Debug, PartialEq, Eq)]
11pub enum Error {
12    /// Age Format Error
13    #[cfg(feature = "age")]
14    AgeFormatError(crate::keys::age::DecError),
15    /// Bip39 Error
16    #[cfg(feature = "bip39")]
17    Bip39Error(crate::keys::bip39::Error),
18    /// Bip44 Bad Purpose Segment
19    #[cfg(feature = "bip44")]
20    Bip44Error(crate::keys::bip44::BadPurpose),
21    /// Buffer Error
22    BufferSize {
23        name: &'static str,
24        needs: usize,
25        has: usize,
26    },
27    /// Cipher Error
28    CipherError { alg: &'static str },
29    /// Signature Error
30    SignatureError { alg: &'static str },
31    /// Conversion Error
32    ConvertError { from: &'static str, to: &'static str },
33    /// Private Key Error
34    PrivateKeyError,
35    /// InvalidArgumentError
36    InvalidArgumentError { alg: &'static str, expected: &'static str },
37    #[cfg(feature = "slip10")]
38    /// Slip10 Segment Hardening Error
39    Slip10Error(crate::keys::slip10::SegmentHardeningError),
40    /// System Error
41    SystemError {
42        call: &'static str,
43        raw_os_error: Option<i32>,
44    },
45}
46
47impl Display for Error {
48    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
49        match self {
50            #[cfg(feature = "age")]
51            Error::AgeFormatError(inner) => write!(f, "failed to decode/decrypt age format: {inner:?}"),
52            #[cfg(feature = "bip39")]
53            Error::Bip39Error(inner) => write!(f, "bip39 error: {inner:?}"),
54            #[cfg(feature = "bip44")]
55            Error::Bip44Error(inner) => write!(f, "bip44 error: {inner:?}"),
56            Error::BufferSize { name, needs, has } => {
57                write!(f, "{} buffer needs {} bytes, but it only has {}", name, needs, has)
58            }
59            Error::CipherError { alg } => write!(f, "error in algorithm {}", alg),
60            Error::SignatureError { alg } => write!(f, "error in signature algorithm {}", alg),
61            Error::ConvertError { from, to } => write!(f, "failed to convert {} to {}", from, to),
62            Error::PrivateKeyError => write!(f, "Failed to generate private key."),
63            Error::InvalidArgumentError { alg, expected } => write!(f, "{} expects {}", alg, expected),
64            #[cfg(feature = "slip10")]
65            Error::Slip10Error(inner) => write!(f, "slip10 error: {inner:?}"),
66            Error::SystemError {
67                call,
68                raw_os_error: None,
69            } => write!(f, "system error when calling {}", call),
70            Error::SystemError {
71                call,
72                raw_os_error: Some(errno),
73            } => write!(f, "system error when calling {}: {}", call, errno),
74        }
75    }
76}
77
78#[cfg(feature = "std")]
79impl std::error::Error for Error {}