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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
//! Error types for runtimes.
pub use oasis_core_runtime::types::Error as RuntimeError;
use crate::{dispatcher, module::CallResult};
/// A runtime 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-runtime-sdk-macros")]
/// # mod example {
/// # use oasis_runtime_sdk_macros::Error;
/// const MODULE_NAME: &str = "my-module";
/// #[derive(Clone, Debug, Error, thiserror::Error)]
/// #[sdk_error(autonumber)] // `module_name` meta is required if `MODULE_NAME` isn't in scope
/// 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 a call result.
fn into_call_result(self) -> CallResult
where
Self: Sized,
{
match self.into_abort() {
Ok(err) => CallResult::Aborted(err),
Err(failed) => CallResult::Failed {
module: failed.module_name().to_owned(),
code: failed.code(),
message: failed.to_string(),
},
}
}
/// Consumes self and returns either `Ok(err)` (where `err` is a dispatcher error) when batch
/// should abort or `Err(self)` when this is just a regular error.
fn into_abort(self) -> Result<dispatcher::Error, Self>
where
Self: Sized,
{
Err(self)
}
}
impl Error for std::convert::Infallible {
fn module_name(&self) -> &str {
"(none)"
}
fn code(&self) -> u32 {
Default::default()
}
}
#[cfg(test)]
mod test {
use super::*;
const MODULE_NAME_1: &str = "test1";
const MODULE_NAME_2: &str = "test2";
#[derive(thiserror::Error, Debug, oasis_runtime_sdk_macros::Error)]
#[sdk_error(module_name = "MODULE_NAME_1")]
enum ChildError {
#[error("first error")]
#[sdk_error(code = 1)]
Error1,
#[error("second error")]
#[sdk_error(code = 2)]
Error2,
}
#[derive(thiserror::Error, Debug, oasis_runtime_sdk_macros::Error)]
#[sdk_error(module_name = "MODULE_NAME_2")]
enum ParentError {
#[error("first error")]
#[sdk_error(code = 1)]
NotForwarded(#[source] ChildError),
#[error("nested error")]
#[sdk_error(transparent)]
Nested(#[source] ChildError),
}
#[derive(thiserror::Error, Debug, oasis_runtime_sdk_macros::Error)]
enum ParentParentError {
#[error("nested nested error")]
#[sdk_error(transparent)]
Nested(#[source] ParentError),
}
#[test]
fn test_error_sources_1() {
let err = ParentError::Nested(ChildError::Error1);
let result = err.into_call_result();
match result {
CallResult::Failed {
module,
code,
message: _,
} => {
assert_eq!(module, "test1");
assert_eq!(code, 1);
}
_ => panic!("expected failed result, got: {:?}", result),
}
let err = ParentError::Nested(ChildError::Error2);
let result = err.into_call_result();
match result {
CallResult::Failed {
module,
code,
message: _,
} => {
assert_eq!(module, "test1");
assert_eq!(code, 2);
}
_ => panic!("expected failed result, got: {:?}", result),
}
}
#[test]
fn test_error_sources_2() {
let err = ParentError::NotForwarded(ChildError::Error1);
let result = err.into_call_result();
match result {
CallResult::Failed {
module,
code,
message: _,
} => {
assert_eq!(module, "test2");
assert_eq!(code, 1);
}
_ => panic!("expected failed result, got: {:?}", result),
}
let err = ParentError::NotForwarded(ChildError::Error2);
let result = err.into_call_result();
match result {
CallResult::Failed {
module,
code,
message: _,
} => {
assert_eq!(module, "test2");
assert_eq!(code, 1);
}
_ => panic!("expected failed result, got: {:?}", result),
}
}
#[test]
fn test_error_sources_3() {
let err = ParentParentError::Nested(ParentError::Nested(ChildError::Error1));
let result = err.into_call_result();
match result {
CallResult::Failed {
module,
code,
message: _,
} => {
assert_eq!(module, "test1");
assert_eq!(code, 1);
}
_ => panic!("expected failed result, got: {:?}", result),
}
}
}