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
//! Contract error trait.
use crate::types::ExecutionResult;

/// A contract error that gets propagated to the caller.
///
/// It extends `std::error::Error` with module name and error code so that errors can be easily
/// serialized and transferred between different processes.
///
/// This trait can be derived:
/// ```
/// # #[cfg(feature = "oasis-contract-sdk-macros")]
/// # mod example {
/// # use oasis_contract_sdk_macros::Error;
/// #[derive(Clone, Debug, Error, thiserror::Error)]
/// #[sdk_error(autonumber)]
/// enum Error {
///    #[error("invalid argument")]
///    InvalidArgument,          // autonumbered to 0
///
///    #[error("forbidden")]
///    #[sdk_error(code = 401)]  // manually numbered to 403 (`code` or autonumbering is required)
///    Forbidden,
/// }
/// # }
/// ```
pub trait Error: std::error::Error {
    /// Name of the module that emitted the error.
    fn module_name(&self) -> &str;

    /// Error code uniquely identifying the error.
    fn code(&self) -> u32;

    /// Converts the error into an execution result.
    fn to_execution_result(&self) -> ExecutionResult {
        ExecutionResult::Failed {
            module: self.module_name().to_owned(),
            code: self.code(),
            message: self.to_string(),
        }
    }
}

impl Error for std::convert::Infallible {
    fn module_name(&self) -> &str {
        "(none)"
    }

    fn code(&self) -> u32 {
        Default::default()
    }
}