clique_sibyl_commonlib/
errors.rs

1use serde_json::{json, Value};
2use thiserror::Error;
3
4#[derive(Error, Debug)]
5pub enum SibylError {
6    #[error(transparent)]
7    StdIOError(#[from] std::io::Error),
8
9    #[error(transparent)]
10    StdStrUtf8Error(#[from] std::str::Utf8Error),
11
12    #[error(transparent)]
13    StdStringFromUtf8Error(#[from] std::string::FromUtf8Error),
14
15    #[error(transparent)]
16    StdParseIntError(#[from] std::num::ParseIntError),
17
18    #[error(transparent)]
19    SerdeJsonError(#[from] serde_json::Error),
20
21    #[cfg(any(
22        feature = "rustls-0_20",
23        feature = "rustls-0_21",
24        feature = "rustls-0_22",
25        feature = "rustls-0_23"
26    ))]
27    #[error(transparent)]
28    TLSError(#[from] crate::rustls::Error),
29
30    #[error(transparent)]
31    SystemTimeError(#[from] std::time::SystemTimeError),
32
33    #[error(transparent)]
34    Base64DecodeError(#[from] base64::DecodeError),
35
36    #[error(transparent)]
37    HexFromHexError(#[from] hex::FromHexError),
38
39    #[error(transparent)]
40    PemError(#[from] pem::PemError),
41
42    #[error(transparent)]
43    SignatureError(#[from] signature::Error),
44
45    #[error("Error message: {0}")]
46    Error(&'static str),
47
48    #[error("NetworkError with code: {:}, data: {:}", code, data.to_string())]
49    NetworkError { code: u16, data: Value },
50}
51
52impl SibylError {
53    pub fn custom<T: ToString>(s: T) -> Self {
54        Self::custom_with_code(400, s)
55    }
56
57    pub fn custom_with_code<T: ToString>(code: u16, s: T) -> Self {
58        Self::NetworkError { code, data: json!({ "message": s.to_string() }) }
59    }
60}
61
62impl From<&'static str> for SibylError {
63    fn from(err: &'static str) -> SibylError {
64        SibylError::Error(err)
65    }
66}
67
68pub type Result<T> = std::result::Result<T, SibylError>;