ddk_manager/
error.rs

1//! #Error
2use std::fmt;
3
4/// An error code.
5#[derive(Debug)]
6pub enum Error {
7    /// Error that occured while converting from DLC message to internal
8    /// representation.
9    Conversion(crate::conversion_utils::Error),
10    /// An IO error.
11    IOError(lightning::io::Error),
12    /// Deserialize error
13    Deserialize(bitcoin::consensus::encode::Error),
14    /// Some invalid parameters were provided.
15    InvalidParameters(String),
16    /// An invalid state was encounter, likely to indicate a bug.
17    InvalidState(String),
18    /// An error occurred in the wallet component.
19    WalletError(Box<dyn std::error::Error + Send + Sync + 'static>),
20    /// An error occurred in the blockchain component.
21    BlockchainError(String),
22    /// The storage component encountered an error.
23    StorageError(String),
24    /// The oracle component encountered an error.
25    OracleError(String),
26    /// An error occurred in the DLC library.
27    DlcError(ddk_dlc::Error),
28    /// An error occurred in the Secp library.
29    SecpError(secp256k1_zkp::Error),
30}
31
32impl fmt::Display for Error {
33    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
34        match *self {
35            Error::Conversion(_) => write!(f, "Conversion error"),
36            Error::IOError(_) => write!(f, "IO error"),
37            Error::Deserialize(ref s) => write!(f, "Deserialize error: {s}"),
38            Error::InvalidState(ref s) => write!(f, "Invalid state: {s}"),
39            Error::InvalidParameters(ref s) => write!(f, "Invalid parameters were provided: {s}"),
40            Error::WalletError(ref e) => write!(f, "Wallet error {e}"),
41            Error::BlockchainError(ref s) => write!(f, "Blockchain error {s}"),
42            Error::StorageError(ref s) => write!(f, "Storage error {s}"),
43            Error::DlcError(ref e) => write!(f, "Dlc error {e}"),
44            Error::OracleError(ref s) => write!(f, "Oracle error {s}"),
45            Error::SecpError(_) => write!(f, "Secp error"),
46        }
47    }
48}
49
50impl From<lightning::io::Error> for Error {
51    fn from(e: lightning::io::Error) -> Error {
52        Error::IOError(e)
53    }
54}
55
56impl From<ddk_dlc::Error> for Error {
57    fn from(e: ddk_dlc::Error) -> Error {
58        Error::DlcError(e)
59    }
60}
61
62impl From<crate::conversion_utils::Error> for Error {
63    fn from(e: crate::conversion_utils::Error) -> Error {
64        Error::Conversion(e)
65    }
66}
67
68impl From<secp256k1_zkp::Error> for Error {
69    fn from(e: secp256k1_zkp::Error) -> Error {
70        Error::SecpError(e)
71    }
72}
73
74impl From<secp256k1_zkp::UpstreamError> for Error {
75    fn from(e: secp256k1_zkp::UpstreamError) -> Error {
76        Error::SecpError(secp256k1_zkp::Error::Upstream(e))
77    }
78}
79
80impl From<bitcoin::consensus::encode::Error> for Error {
81    fn from(e: bitcoin::consensus::encode::Error) -> Self {
82        Error::Deserialize(e)
83    }
84}
85
86#[cfg(feature = "std")]
87impl std::error::Error for Error {
88    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
89        match self {
90            Error::Conversion(e) => Some(e),
91            Error::IOError(e) => Some(e),
92            Error::Deserialize(e) => Some(e),
93            Error::InvalidParameters(_) => None,
94            Error::InvalidState(_) => None,
95            Error::WalletError(_) => None,
96            Error::BlockchainError(_) => None,
97            Error::StorageError(_) => None,
98            Error::OracleError(_) => None,
99            Error::DlcError(e) => Some(e),
100            Error::SecpError(e) => Some(e),
101        }
102    }
103}