Skip to content

Commit

Permalink
Add error handling
Browse files Browse the repository at this point in the history
Signed-off-by: Glenn Lewis <[email protected]>
  • Loading branch information
gmlewis committed Jul 9, 2024
1 parent 0c1bf5f commit 5a8ea5c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
36 changes: 34 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion moon.mod.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "extism/moonbit-pdk",
"version": "0.16.0",
"version": "0.17.0",
"deps": {
"gmlewis/json": "0.11.0"
},
Expand Down
12 changes: 12 additions & 0 deletions pdk/host/host.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 5a8ea5c

Please sign in to comment.