1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use crate::cfg_client;

use serde_json::Error as SerdeJsonError;
use std::error::Error as StdError;
use std::fmt;
use std::fmt::Debug;

#[derive(Debug)]
pub enum Error {
    // only import the reqwest library when the client feature is enabled
    #[cfg(feature = "client")]
    #[cfg_attr(doc_cfg, doc(cfg(feature = "client")))]
    HttpError {
        status_code: reqwest::StatusCode,
        status_text: String,
        source: reqwest::Error,
    },
    EnvVariableMissing(String),
    CustomMessage(String),
    CustomError {
        message: String,
        source: Box<dyn StdError + 'static>,
    },
    // Generics
    Generic,
    SgxError,
    SgxWriteError,
    QuoteParseError,
    DockerError,
    BollardError,
    ContainerStartError,
    ContainerCreateError,
    AttachError,
    ContainerResultParseError,
    FunctionResultParseError,
    IllegalFunctionOutput,
    NetworkError,
    TxFailure,
    InvalidQuoteError,
    TxCompileErr,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            #[cfg(feature = "client")]
            #[cfg_attr(doc_cfg, doc(cfg(feature = "client")))]
            Error::HttpError {
                status_code,
                status_text,
                ..
            } => write!(
                f,
                "Reqwest error: {} - {}",
                status_code.as_str(),
                status_text
            ),

            Error::EnvVariableMissing(message) => {
                write!(f, "Env variable missing {}", message.as_str())
            }
            Error::CustomMessage(message) => write!(f, "error: {}", message.as_str()),
            Error::CustomError {
                message, source, ..
            } => write!(f, "error: {} - {:?}", message.as_str(), source),
            // Handle other error variants as needed
            Error::Generic => write!(f, "Generic Error"),
            Error::SgxError => write!(f, "Sgx Error"),
            Error::SgxWriteError => write!(f, "SgxWriteError"),
            Error::QuoteParseError => write!(f, "QuoteParseError"),
            Error::DockerError => write!(f, "DockerError"),
            Error::BollardError => write!(f, "BollardError"),
            Error::ContainerStartError => write!(f, "ContainerStartError"),
            Error::ContainerCreateError => write!(f, "ContainerCreateError"),
            Error::AttachError => write!(f, "AttachError"),
            Error::ContainerResultParseError => write!(f, "ContainerResultParseError"),
            Error::FunctionResultParseError => write!(f, "FunctionResultParseError"),
            Error::IllegalFunctionOutput => write!(f, "IllegalFunctionOutput"),
            Error::NetworkError => write!(f, "NetworkError"),
            Error::TxFailure => write!(f, "TxFailure"),
            Error::InvalidQuoteError => write!(f, "InvalidQuoteError"),
            Error::TxCompileErr => write!(f, "TxCompileErr"),
        }
    }
}

impl From<&str> for Error {
    fn from(error: &str) -> Self {
        Error::CustomMessage(error.to_string())
    }
}

impl From<hex::FromHexError> for Error {
    fn from(error: hex::FromHexError) -> Self {
        Error::CustomError {
            message: "hex error".to_string(),
            source: Box::new(error),
        }
    }
}

impl StdError for Error {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match self {
            Error::CustomError { source, .. } => Some(source.as_ref()), // Handle other error variants as needed
            _ => None,
        }
    }
}

impl From<SerdeJsonError> for Error {
    fn from(error: SerdeJsonError) -> Self {
        Error::CustomError {
            message: "serde_json error".to_string(),
            source: Box::new(error),
        }
    }
}

cfg_client! {
    impl From<reqwest::Error> for Error {
        fn from(error: reqwest::Error) -> Self {
            if let Some(status) = error.status() {
                Error::HttpError {
                    status_code: status,
                    status_text: status.canonical_reason().unwrap_or("Unknown").to_string(),
                    source: error,
                }
            } else {
                // You can choose to handle non-HTTP errors differently or use the same variant
                Error::HttpError {
                    status_code: reqwest::StatusCode::default(),
                    status_text: "Non-HTTP error".to_string(),
                    source: error,
                }
            }
        }
    }
}