Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hook up error propagation #381

Merged
merged 4 commits into from
Apr 22, 2019
Merged

Conversation

lachlansneff
Copy link
Contributor

You can now retrieve the error from a panicking or error-returning imported function.

Example:

#[derive(Debug)]
struct ExitCode {
    code: i32,
}

fn do_panic(_ctx: &mut Ctx) -> Result<i32, ExitCode> {
    Err(ExitCode { code: 42 })
}

// This wasm functions calls `do_panic`.
let foo: Func<(), i32> = instance.func(...)?;

let result = foo.call();

println!("result: {:?}", result);

if let Err(RuntimeError::Error { data }) = result {
    if let Ok(exit_code) = data.downcast::<ExitCode>() {
        println!("exit code: {:?}", exit_code);
    }
}

outputs:

result: Err(unknown error)
exit code: ExitCode { code: 42 }

@lachlansneff
Copy link
Contributor Author

@kanaka This pr will solve #373 once it's hooked up to the wasmer cli.

@@ -63,5 +70,11 @@ fn main() -> Result<(), error::Error> {

println!("result: {:?}", result);

if let Err(RuntimeError::Error { data }) = result {
if let Ok(exit_code) = data.downcast::<ExitCode>() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this.

@lachlansneff
Copy link
Contributor Author

bors try

bors bot added a commit that referenced this pull request Apr 22, 2019
@bors
Copy link
Contributor

bors bot commented Apr 22, 2019

@lachlansneff
Copy link
Contributor Author

bors r+

bors bot added a commit that referenced this pull request Apr 22, 2019
381: Hook up error propagation r=lachlansneff a=lachlansneff

You can now retrieve the error from a panicking or error-returning imported function.

Example:

```rust
#[derive(Debug)]
struct ExitCode {
    code: i32,
}

fn do_panic(_ctx: &mut Ctx) -> Result<i32, ExitCode> {
    Err(ExitCode { code: 42 })
}

// This wasm functions calls `do_panic`.
let foo: Func<(), i32> = instance.func(...)?;

let result = foo.call();

println!("result: {:?}", result);

if let Err(RuntimeError::Error { data }) = result {
    if let Ok(exit_code) = data.downcast::<ExitCode>() {
        println!("exit code: {:?}", exit_code);
    }
}
```

outputs:

```
result: Err(unknown error)
exit code: ExitCode { code: 42 }
```

Co-authored-by: Lachlan Sneff <[email protected]>
@bors
Copy link
Contributor

bors bot commented Apr 22, 2019

@bors bors bot merged commit ff9de18 into master Apr 22, 2019
@bors bors bot deleted the feature/retrievable-error-propagation branch April 22, 2019 23:08
surban pushed a commit to rust-wasi-web/wwrr that referenced this pull request Nov 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants