From 5a8ea5c7d2bc423f754b9e63ff2d4794720c6ec4 Mon Sep 17 00:00:00 2001 From: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> Date: Tue, 9 Jul 2024 09:56:24 -0400 Subject: [PATCH] Add error handling Signed-off-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- README.md | 36 ++++++++++++++++++++++++++++++++++-- moon.mod.json | 2 +- pdk/host/host.mbt | 12 ++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a2f64f9..c09cbef 100644 --- a/README.md +++ b/README.md @@ -76,8 +76,8 @@ Now paste this into your `main/main.mbt` file: ```rust pub fn greet() -> Int { - let input = @host.input_string() - let greeting = "Hello, \(input)!" + let name = @host.input_string() + let greeting = "Hello, \(name)!" @host.output_string(greeting) 0 // success } @@ -130,6 +130,38 @@ extism call target/wasm/release/build/main/main.wasm greet --input "Benjamin" -- > **Note**: We also have a web-based, plug-in tester called the [Extism Playground](https://playground.extism.org/) +### More Exports: Error Handling + +Suppose we want to re-write our greeting module to never greet Benjamins. +We can use [`@host.set_error`](https://mooncakes.io/docs/#/extism/moonbit-pdk/pdk/host/members?id=set_error): + +```rust +pub fn greet() -> Int { + let name = @host.input_string() + if name == "Benjamin" { + @host.set_error("Sorry, we don't greet Benjamins!") + return 1 // failure + } + let greeting = "Hello, \(name)!" + @host.output_string(greeting) + 0 // success +} +``` + +Now when we try again: + +```bash +moon build --target wasm +extism call target/wasm/release/build/main/main.wasm greet --input "Benjamin" --wasi +# => Error: Sorry, we don't greet Benjamins! +echo $? # print last status code +# => 1 +extism call target/wasm/release/build/main/main.wasm greet --input "Zach" --wasi +# => Hello, Zach! +echo $? +# => 0 +``` + ## For PDK Devs: Building the PDK locally Before building, you must have already installed the MoonBit programming language, diff --git a/moon.mod.json b/moon.mod.json index 523fc6a..c947185 100644 --- a/moon.mod.json +++ b/moon.mod.json @@ -1,6 +1,6 @@ { "name": "extism/moonbit-pdk", - "version": "0.16.0", + "version": "0.17.0", "deps": { "gmlewis/json": "0.11.0" }, diff --git a/pdk/host/host.mbt b/pdk/host/host.mbt index f29a91e..e2a4df1 100644 --- a/pdk/host/host.mbt +++ b/pdk/host/host.mbt @@ -42,6 +42,18 @@ pub fn output_json_value(j : @json.JsonValue) -> Unit { @jsonutil.stringify(j, spaces=0, newline=false) |> output_string() } +fn set_error_bytes(b : Bytes) -> Unit { + let mem = output_bytes_to_memory(b) + @extism.error_set(mem.offset) + mem.free() +} + +/// `set_error` converts a MoonBit String (UTF-16) to an Extism string (UTF-8) +/// and sends it to the host as its error output. +pub fn set_error(s : String) -> Unit { + @pdk.ToUtf8::to_utf8(s) |> set_error_bytes() +} + /// `log_warn_str` is a helper function to log a warn string to the host. pub fn log_warn_str(s : String) -> Unit { let { offset, .. } = @pdk.ToUtf8::to_utf8(s) |> output_bytes_to_memory()