Skip to content

Commit 8de2185

Browse files
authored
Merge pull request #401 from Shopify/js.return-error-when-module-errors
optionally return non-zero error code when function run sees error
2 parents f031b8e + 41d5220 commit 8de2185

File tree

15 files changed

+132
-20
lines changed

15 files changed

+132
-20
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ members = [
33
"tests/fixtures/exit_code",
44
"tests/fixtures/log_truncation_function",
55
"tests/fixtures/exports",
6+
"tests/fixtures/noop",
67
]
78

89
[package]

src/engine.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ pub fn run(params: FunctionRunParams) -> Result<FunctionRunResult> {
136136
let memory_usage: u64;
137137
let instructions: u64;
138138
let mut error_logs: String = String::new();
139+
let mut module_result: Result<(), anyhow::Error>;
139140
let profile_data: Option<String>;
140141

141142
{
@@ -158,7 +159,6 @@ pub fn run(params: FunctionRunParams) -> Result<FunctionRunResult> {
158159

159160
let func = instance.get_typed_func::<(), ()>(store.as_context_mut(), export)?;
160161

161-
let module_result;
162162
(module_result, profile_data) = if let Some(profile_opts) = profile_opts {
163163
let (result, profile_data) = wasmprof::ProfilerBuilder::new(&mut store)
164164
.frequency(profile_opts.interval)
@@ -176,7 +176,7 @@ pub fn run(params: FunctionRunParams) -> Result<FunctionRunResult> {
176176
// modules may exit with a specific exit code, an exit code of 0 is considered success but is reported as
177177
// a GuestFault by wasmtime, so we need to map it to a success result. Any other exit code is considered
178178
// a failure.
179-
let module_result =
179+
module_result =
180180
module_result.or_else(|error| match error.downcast_ref::<wasi_common::I32Exit>() {
181181
Some(I32Exit(0)) => Ok(()),
182182
Some(I32Exit(code)) => Err(anyhow!("module exited with code: {}", code)),
@@ -188,7 +188,7 @@ pub fn run(params: FunctionRunParams) -> Result<FunctionRunResult> {
188188

189189
match module_result {
190190
Ok(_) => {}
191-
Err(e) => {
191+
Err(ref e) => {
192192
error_logs = e.to_string();
193193
}
194194
}
@@ -234,6 +234,7 @@ pub fn run(params: FunctionRunParams) -> Result<FunctionRunResult> {
234234
output,
235235
profile: profile_data,
236236
scale_factor,
237+
success: module_result.is_ok(),
237238
};
238239

239240
Ok(function_run_result)

src/function_run_result.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub struct FunctionRunResult {
3030
pub profile: Option<String>,
3131
#[serde(skip)]
3232
pub scale_factor: f64,
33+
pub success: bool,
3334
}
3435

3536
const DEFAULT_INSTRUCTIONS_LIMIT: u64 = 11_000_000;
@@ -253,6 +254,7 @@ mod tests {
253254
})),
254255
profile: None,
255256
scale_factor: 1.0,
257+
success: true,
256258
};
257259

258260
let predicate = predicates::str::contains("Instructions: 1.001K")
@@ -284,6 +286,7 @@ mod tests {
284286
})),
285287
profile: None,
286288
scale_factor: 1.0,
289+
success: true,
287290
};
288291

289292
let predicate = predicates::str::contains("Instructions: 1")
@@ -311,6 +314,7 @@ mod tests {
311314
})),
312315
profile: None,
313316
scale_factor: 1.0,
317+
success: true,
314318
};
315319

316320
let predicate = predicates::str::contains("Instructions: 999")

src/main.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,5 +196,9 @@ fn main() -> Result<()> {
196196
std::fs::write(profile_opts.unwrap().out, profile)?;
197197
}
198198

199-
Ok(())
199+
if function_run_result.success {
200+
Ok(())
201+
} else {
202+
anyhow::bail!("The Function execution failed. Review the logs for more information.")
203+
}
200204
}

tests/fixtures/README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ Example Functions used as test fixtures.
1111

1212
**Rust examples:**
1313
```
14-
cargo wasi build --profile=wasm -p exit_code -p exports -p log_truncation_function &&
15-
cp target/wasm32-wasi/wasm/{exit_code.wasm,exports.wasm,log_truncation_function.wasm} tests/fixtures/build
14+
cargo wasi build --profile=wasm -p exit_code -p exports -p log_truncation_function -p noop &&
15+
cp target/wasm32-wasi/wasm/{exit_code.wasm,exports.wasm,log_truncation_function.wasm,noop.wasm} tests/fixtures/build
1616
```
1717

1818
**JS examples:**
@@ -31,6 +31,11 @@ js_functions_javy_v1.wasm:
3131
javy build -C dynamic -C plugin=providers/shopify_functions_javy_v1.wasm -o tests/fixtures/build/js_functions_javy_v1.wasm tests/fixtures/js_function/src/functions.js
3232
```
3333

34+
js_function_that_throws.wasm:
35+
```
36+
javy build -C dynamic -C plugin=providers/javy_quickjs_provider_v3.wasm -o tests/fixtures/build/js_function_that_throws.wasm tests/fixtures/js_function_that_throws/src/functions.js
37+
```
38+
3439
**`*.wat` examples:**
3540
```
3641
find tests/fixtures -maxdepth 1 -type f -name "*.wat" \
422 Bytes
Binary file not shown.

tests/fixtures/build/exports.wasm

-7.18 KB
Binary file not shown.
2.65 KB
Binary file not shown.
-21.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)