switchboard_common/
function_error.rs

1// TODO: we may want to reserve [1-99] for errors that are fatal to the function (possibly retryable)
2// then reserve [100-199] for user errors that are not fatal to the function but give more status
3// on-chain to the function consumer.
4#[repr(u8)]
5#[derive(Clone, Debug, Default)]
6pub enum SbFunctionError {
7    #[default]
8    None = 0,
9    // A custom user error between [1 - 198]
10    FunctionError(u8),
11    // An unknown panic from the function container
12    FatalError = 199,
13    // Switchboard reserved errors
14    /// Failed to build a transaction with the emitted FunctionResult
15    FunctionResultEmitError = 200,
16    /// The FunctionResult generated SGX quote failed verification
17    QuoteVerificationError = 201,
18    // Reserved for Switchboard [202-249]
19    SwitchboardError(u8),
20    // Errors [250 - 255] reserved for Switchboard
21    OutOfFunds = 250,
22    Reserved251 = 251,
23    ContainerUnavailable = 252,
24    /// Failed to find the FunctionResult in the emitted container logs
25    FunctionResultNotFound = 253,
26    /// Failed to execute the FunctionResult emitted transaction
27    CallbackError = 254,
28    /// Function failed to complete within the designated timeout
29    FunctionTimeout = 255,
30}
31impl From<u8> for SbFunctionError {
32    fn from(value: u8) -> Self {
33        match value {
34            0 => SbFunctionError::None,
35            1..=198 => SbFunctionError::FunctionError(value),
36            199 => SbFunctionError::FatalError,
37            200 => SbFunctionError::FunctionResultEmitError,
38            201 => SbFunctionError::QuoteVerificationError,
39            202..=249 => SbFunctionError::SwitchboardError(value),
40            250 => SbFunctionError::OutOfFunds,
41            251 => SbFunctionError::Reserved251,
42            252 => SbFunctionError::ContainerUnavailable,
43            253 => SbFunctionError::FunctionResultNotFound,
44            254 => SbFunctionError::CallbackError,
45            255 => SbFunctionError::FunctionTimeout,
46        }
47    }
48}
49impl SbFunctionError {
50    pub fn as_u8(&self) -> u8 {
51        match self {
52            SbFunctionError::None => 0,
53            SbFunctionError::FunctionError(value) => *value,
54            SbFunctionError::FatalError => 199,
55            SbFunctionError::FunctionResultEmitError => 200,
56            SbFunctionError::QuoteVerificationError => 201,
57            SbFunctionError::SwitchboardError(value) => *value,
58            SbFunctionError::OutOfFunds => 250,
59            SbFunctionError::Reserved251 => 251,
60            SbFunctionError::ContainerUnavailable => 252,
61            SbFunctionError::FunctionResultNotFound => 253,
62            SbFunctionError::CallbackError => 254,
63            SbFunctionError::FunctionTimeout => 255,
64        }
65    }
66}