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
140
141
use crate::{cfg_client, cfg_not_client};

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

cfg_client! {
    use reqwest::{Error as ReqwestError, StatusCode};

    #[derive(Debug)]
    pub enum Error {
        CustomMessage(String),
        CustomError {
            message: String,
            source: Box<dyn StdError + 'static>,
        },
        HttpError {
            status_code: StatusCode,
            status_text: String,
            source: ReqwestError,
        },
        // Add other error variants as needed
        SgxError,
        SgxWriteError,
        TxFailure,
        NetworkErr,
        InvalidQuoteError,
        TxCompileErr,
        EnvVariableMissing(String),
    }

    impl fmt::Display for Error {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            match self {
                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::HttpError {
                    status_code,
                    status_text,
                    ..
                } => write!(
                    f,
                    "Reqwest error: {} - {}",
                    status_code.as_str(),
                    status_text
                ),
                Error::SgxError => write!(f, "SGX error"),
                Error::SgxWriteError => write!(f, "SGX write error"),
                Error::TxFailure => write!(f, "Tx failure"),
                Error::NetworkErr => write!(f, "Network error"),
                Error::InvalidQuoteError => write!(f, "Invalid Quote"),
                Error::TxCompileErr => write!(f, "Tx compile error"),
                Error::EnvVariableMissing(message) => {
                    write!(f, "Env variable missing {}", message.as_str())
                }
            }
        }
    }

    impl From<ReqwestError> for Error {
        fn from(error: ReqwestError) -> 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: StatusCode::default(),
                    status_text: "Non-HTTP error".to_string(),
                    source: error,
                }
            }
        }
    }
}

cfg_not_client! {
    #[derive(Debug)]
    pub enum Error {
        CustomMessage(String),
        CustomError {
            message: String,
            source: Box<dyn StdError + 'static>,
        },
        // Add other error variants as needed
        SgxError,
        SgxWriteError,
        TxFailure,
        NetworkErr,
        InvalidQuoteError,
        TxCompileErr,
        EnvVariableMissing(String),
    }


    impl fmt::Display for Error {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            match self {
                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::SgxError => write!(f, "SGX error"),
                Error::SgxWriteError => write!(f, "SGX write error"),
                Error::TxFailure => write!(f, "Tx failure"),
                Error::NetworkErr => write!(f, "Network error"),
                Error::InvalidQuoteError => write!(f, "Invalid Quote"),
                Error::TxCompileErr => write!(f, "Tx compile error"),
                Error::EnvVariableMissing(message) => {
                    write!(f, "Env variable missing {}", message.as_str())
                }
            }
        }
    }
}

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),
        }
    }
}