1use std::fmt::Display;
4use std::num::TryFromIntError;
5use std::time::Duration;
6
7#[derive(thiserror::Error, Debug)]
8pub enum SurrealismError {
9 #[cfg(feature = "host")]
10 #[error("WASM compilation failed: {0}")]
11 Compilation(wasmtime::Error),
12 #[cfg(feature = "host")]
13 #[error("WASM instantiation failed: {0}")]
14 Instantiation(wasmtime::Error),
15 #[error("Function call error: {0}")]
16 FunctionCallError(String),
17 #[error(
18 "Module execution timed out (epoch interrupt). Effective timeout: {effective:?}, context timeout: {context_timeout:?}, module limit: {module_limit:?}"
19 )]
20 Timeout {
21 effective: Option<Duration>,
22 context_timeout: Option<Duration>,
23 module_limit: Option<Duration>,
24 },
25 #[error("Unsupported ABI version: expected {expected}, got {got}")]
26 UnsupportedAbi {
27 expected: u32,
28 got: u32,
29 },
30 #[error("Integer conversion error: {0}")]
31 IntConversion(#[from] TryFromIntError),
32 #[cfg(feature = "host")]
33 #[error("Wasmtime error: {0}")]
34 Wasmtime(#[from] wasmtime::Error),
35 #[error("Other error: {0}")]
36 Other(#[from] anyhow::Error),
37}
38
39impl SurrealismError {
40 pub fn is_trap(&self) -> bool {
47 match self {
48 Self::Timeout {
50 ..
51 } => true,
52 #[cfg(feature = "host")]
54 Self::Instantiation(_) => true,
55 #[cfg(feature = "host")]
59 Self::Wasmtime(_) => true,
60 #[cfg(feature = "host")]
62 Self::Compilation(_) => false,
63 Self::FunctionCallError(_) => false,
65 Self::UnsupportedAbi {
67 ..
68 } => false,
69 Self::IntConversion(_) => false,
70 Self::Other(_) => false,
71 }
72 }
73}
74
75pub type SurrealismResult<T> = std::result::Result<T, SurrealismError>;
76
77pub trait PrefixErr<T> {
78 fn prefix_err<F, S>(self, f: F) -> SurrealismResult<T>
79 where
80 F: FnOnce() -> S,
81 S: Display;
82}
83
84impl<T, E: Display> PrefixErr<T> for std::result::Result<T, E> {
85 fn prefix_err<F, S>(self, f: F) -> SurrealismResult<T>
86 where
87 F: FnOnce() -> S,
88 S: Display,
89 {
90 self.map_err(|e| SurrealismError::Other(anyhow::anyhow!("{}: {}", f(), e)))
91 }
92}