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
// TODO: we may want to reserve [2-99] for errors that are fatal to the function (possibly retryable)
// then reserve [100-199] for user errors that are not fatal to the function but give more status
// on-chain to the function consumer.
#[repr(u8)]
#[derive(Clone, Debug, Default)]
pub enum SbFunctionError {
    #[default]
    None = 0,
    // An unknown panic from the function container
    FatalError = 1,
    // A custom user error between [2 - 199]
    FunctionError(u8),
    // Switchboard reserved errors
    /// Failed to build a transaction with the emitted FunctionResult
    FunctionResultEmitError = 200,
    /// The FunctionResult generated SGX quote failed verification
    QuoteVerificationError = 201,
    // Reserved for Switchboard [202-249]
    SwitchboardError(u8),
    // Errors [250 - 255] reserved for Switchboard
    OutOfFunds = 250,
    Reserved251 = 251,
    ContainerUnavailable = 252,
    /// Failed to find the FunctionResult in the emitted container logs
    FunctionResultNotFound = 253,
    /// Failed to execute the FunctionResult emitted transaction
    CallbackError = 254,
    /// Function failed to complete within the designated timeout
    FunctionTimeout = 255,
}
impl From<u8> for SbFunctionError {
    fn from(value: u8) -> Self {
        match value {
            0 => SbFunctionError::None,
            1 => SbFunctionError::FatalError,
            2..=199 => SbFunctionError::FunctionError(value),
            200 => SbFunctionError::FunctionResultEmitError,
            201 => SbFunctionError::QuoteVerificationError,
            202..=249 => SbFunctionError::SwitchboardError(value),
            250 => SbFunctionError::OutOfFunds,
            251 => SbFunctionError::Reserved251,
            252 => SbFunctionError::ContainerUnavailable,
            253 => SbFunctionError::FunctionResultNotFound,
            254 => SbFunctionError::CallbackError,
            255 => SbFunctionError::FunctionTimeout,
        }
    }
}
impl SbFunctionError {
    pub fn as_u8(&self) -> u8 {
        match self {
            SbFunctionError::None => 0,
            SbFunctionError::FatalError => 1,
            SbFunctionError::FunctionError(value) => *value,
            SbFunctionError::FunctionResultEmitError => 200,
            SbFunctionError::QuoteVerificationError => 201,
            SbFunctionError::SwitchboardError(value) => *value,
            SbFunctionError::OutOfFunds => 250,
            SbFunctionError::Reserved251 => 251,
            SbFunctionError::ContainerUnavailable => 252,
            SbFunctionError::FunctionResultNotFound => 253,
            SbFunctionError::CallbackError => 254,
            SbFunctionError::FunctionTimeout => 255,
        }
    }
}