dlc_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(dlc::Error),
28    /// An error occurred in the Secp library.
29    SecpError(secp256k1_zkp::Error),
30    /// A computation was out of range.
31    OutOfRange,
32}
33
34impl fmt::Display for Error {
35    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36        match *self {
37            Error::Conversion(_) => write!(f, "Conversion error"),
38            Error::IOError(_) => write!(f, "IO error"),
39            Error::Deserialize(ref s) => write!(f, "Deserialize error: {}", s),
40            Error::InvalidState(ref s) => write!(f, "Invalid state: {}", s),
41            Error::InvalidParameters(ref s) => write!(f, "Invalid parameters were provided: {}", s),
42            Error::WalletError(ref e) => write!(f, "Wallet error {}", e),
43            Error::BlockchainError(ref s) => write!(f, "Blockchain error {}", s),
44            Error::StorageError(ref s) => write!(f, "Storage error {}", s),
45            Error::DlcError(ref e) => write!(f, "Dlc error {}", e),
46            Error::OracleError(ref s) => write!(f, "Oracle error {}", s),
47            Error::SecpError(_) => write!(f, "Secp error"),
48            Error::OutOfRange => write!(f, "Out of range error"),
49        }
50    }
51}
52
53impl From<lightning::io::Error> for Error {
54    fn from(e: lightning::io::Error) -> Error {
55        Error::IOError(e)
56    }
57}
58
59impl From<dlc::Error> for Error {
60    fn from(e: dlc::Error) -> Error {
61        Error::DlcError(e)
62    }
63}
64
65impl From<crate::conversion_utils::Error> for Error {
66    fn from(e: crate::conversion_utils::Error) -> Error {
67        Error::Conversion(e)
68    }
69}
70
71impl From<secp256k1_zkp::Error> for Error {
72    fn from(e: secp256k1_zkp::Error) -> Error {
73        Error::SecpError(e)
74    }
75}
76
77impl From<secp256k1_zkp::UpstreamError> for Error {
78    fn from(e: secp256k1_zkp::UpstreamError) -> Error {
79        Error::SecpError(secp256k1_zkp::Error::Upstream(e))
80    }
81}
82
83impl From<bitcoin::consensus::encode::Error> for Error {
84    fn from(e: bitcoin::consensus::encode::Error) -> Self {
85        Error::Deserialize(e)
86    }
87}
88
89#[cfg(feature = "std")]
90impl std::error::Error for Error {
91    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
92        match self {
93            Error::Conversion(e) => Some(e),
94            Error::IOError(e) => Some(e),
95            Error::Deserialize(e) => Some(e),
96            Error::InvalidParameters(_) => None,
97            Error::InvalidState(_) => None,
98            Error::WalletError(_) => None,
99            Error::BlockchainError(_) => None,
100            Error::StorageError(_) => None,
101            Error::OracleError(_) => None,
102            Error::DlcError(e) => Some(e),
103            Error::SecpError(e) => Some(e),
104            Error::OutOfRange => None,
105        }
106    }
107}