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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use serde_json::Error as SerdeJsonError;
use std::error::Error as StdError;
use std::fmt;
use std::fmt::Debug;
use std::sync::Arc;

#[derive(Clone, Debug)]
pub enum Error {
    // Generics
    Generic,

    CustomMessage(String),
    CustomError {
        message: String,
        source: Arc<dyn StdError + 'static>,
    },
    Unexpected,
    // Environment Errors
    EnvVariableMissing(String),
    InvalidKeypairFile,
    KeyParseError,

    // SGX Errors
    SgxError,
    SgxWriteError,

    // Network Errors
    NetworkError,

    // Quote Errors
    QuoteParseError,
    InvalidQuoteError,

    // Docker/Container Errors
    DockerError,
    ContainerStartError,
    ContainerCreateError,
    ContainerResultParseError,
    AttachError,

    // Function Errors
    FunctionResultParseError,
    IllegalFunctionOutput,
    FunctionVerifyFailure,
    FunctionResultIllegalAccount,
    FunctionResultAccountsMismatch,
    FunctionResultInvalidData,
    FunctionResultInvalidPid,
    FunctionResultEmptyInstructions,

    // Transaction Errors
    TxFailure,
    TxCompileErr,
    TxDeserializationError,
    QvnTxSendFailure,
    InvalidInstructionError,

    // Chain specific Errors
    AnchorParse,
    AnchorParseError,
    EvmError,

    // Misc
    IpfsParseError,
    IpfsNetworkError,
    HeartbeatRoutineFailure,
    EventListenerRoutineFailure,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            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
            _ => write!(f, "{:#?}", self),
        }
    }
}

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: Arc::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: Arc::new(error),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn display_generic() {
        let error = Error::Generic;
        assert_eq!(format!("{}", error), "Generic");
    }

    #[test]
    fn display_custom_message() {
        let error = Error::CustomMessage("my custom message".to_string());
        assert_eq!(format!("{}", error), "error: my custom message");
    }

    #[test]
    fn display_env_variable_missing() {
        let error = Error::EnvVariableMissing("MY_ENV_VAR".to_string());
        assert_eq!(format!("{}", error), "Env variable missing: MY_ENV_VAR");
    }

    #[test]
    fn from_str() {
        let error: Error = "my custom message".into();
        assert_eq!(format!("{}", error), "error: my custom message");
    }

    #[test]
    fn from_hex_error() {
        let hex_error = hex::FromHexError::OddLength;
        let error: Error = hex_error.into();
        assert_eq!(format!("{}", error), "error: hex error - OddLength");
    }

    #[test]
    fn from_serde_json_error() {
        let json = "\"";
        let serde_json_error = serde_json::from_str::<serde_json::Value>(json).unwrap_err();
        let error: Error = serde_json_error.into();
        assert!(format!("{}", error).starts_with("error: serde_json error - "));
    }
}