scripting/runtime/
error.rs1use thiserror::Error;
4
5use super::{EvalValue, HostCondition, ScriptRaise};
6
7#[derive(Debug, Error, PartialEq)]
8pub enum EngineError {
9 #[error("engine config rejected: {0}")]
10 Config(String),
11 #[error("module cache lock poisoned")]
12 CachePoisoned,
13 #[error("module compilation failed: {0}")]
14 Compile(String),
15 #[error("module instantiation failed: {0}")]
16 Instantiate(String),
17 #[error("fuel configuration failed: {0}")]
18 Fuel(String),
19 #[error("missing export `{0}`")]
20 MissingExport(String),
21 #[error("fuel exhausted before completion")]
22 OutOfFuel,
23 #[error("epoch deadline reached before completion")]
24 EpochInterrupt,
25 #[error("store limit of fibers has been reached")]
26 FiberLimit,
27 #[error("script raised {code}: {message}")]
38 ScriptRaised {
39 code: String,
40 message: String,
41 detail: Option<EvalValue>,
42 },
43 #[error("{message}")]
46 HostCondition {
47 code: String,
48 message: String,
49 detail: Option<EvalValue>,
50 },
51 #[error("execution trapped: {0}")]
52 Trap(String),
53}
54
55const FIBER_LIMIT_MESSAGE: &str = "store limit of fibers has been reached";
58
59pub fn classify_runtime_error(err: &wasmtime::Error) -> EngineError {
64 if let Some(trap) = err.downcast_ref::<wasmtime::Trap>() {
65 match *trap {
66 wasmtime::Trap::OutOfFuel => return EngineError::OutOfFuel,
67 wasmtime::Trap::Interrupt => return EngineError::EpochInterrupt,
68 _ => {}
69 }
70 }
71 if let Some(condition) = err
72 .chain()
73 .find_map(|cause| cause.downcast_ref::<HostCondition>())
74 {
75 return EngineError::ScriptRaised {
76 code: condition.code.to_uppercase(),
77 message: condition.message.clone(),
78 detail: condition.detail.clone(),
79 };
80 }
81 if let Some(raise) = err
82 .chain()
83 .find_map(|cause| cause.downcast_ref::<ScriptRaise>())
84 {
85 return EngineError::ScriptRaised {
86 code: raise.code.clone(),
87 message: raise.message.clone(),
88 detail: raise.detail.clone(),
89 };
90 }
91 if err
92 .chain()
93 .any(|cause| cause.to_string() == FIBER_LIMIT_MESSAGE)
94 {
95 return EngineError::FiberLimit;
96 }
97 let mut combined = err.to_string();
101 for cause in err.chain().skip(1) {
102 combined.push_str(": ");
103 combined.push_str(&cause.to_string());
104 }
105 EngineError::Trap(combined)
106}
107
108#[must_use]
121pub fn err_code_and_message(err: &EngineError) -> (String, String) {
122 match err {
123 EngineError::ScriptRaised { code, message, .. } => (code.clone(), message.clone()),
124 EngineError::HostCondition { code, message, .. } => (code.clone(), message.clone()),
125 EngineError::Trap(msg) => ("runtime".to_string(), msg.clone()),
126 EngineError::Compile(msg) => ("compile".to_string(), msg.clone()),
127 EngineError::Instantiate(msg) => ("runtime".to_string(), msg.clone()),
128 EngineError::Fuel(msg) => ("runtime".to_string(), msg.clone()),
129 EngineError::MissingExport(msg) => ("runtime".to_string(), msg.clone()),
130 EngineError::Config(msg) => ("runtime".to_string(), msg.clone()),
131 EngineError::CachePoisoned => (
132 "runtime".to_string(),
133 "module cache lock poisoned".to_string(),
134 ),
135 EngineError::OutOfFuel => ("runtime".to_string(), "fuel exhausted".to_string()),
136 EngineError::EpochInterrupt => ("runtime".to_string(), "epoch deadline".to_string()),
137 EngineError::FiberLimit => ("runtime".to_string(), "fiber limit reached".to_string()),
138 }
139}