Skip to content

Commit

Permalink
Merge #2026
Browse files Browse the repository at this point in the history
2026: expose trap error from RuntimeError r=syrusakbary a=ailisp

<!-- 
Prior to submitting a PR, review the CONTRIBUTING.md document for recommendations on how to test:
https://github.com/wasmerio/wasmer/blob/master/CONTRIBUTING.md#pull-requests

-->

# Description
<!-- 
Provide details regarding the change including motivation,
links to related issues, and the context of the PR.
-->

Internal of RuntimeError is private, user can know UserError via `downcast`, but user isn't able to tell the trap code if it's a `Trap`. This PR expose trap code via `RuntimeError#to_trap`. (And, if both downcast returns error and to_trap returns None, user will know it's a `RuntimeError::Generic`, which can be shown by `error.message()`, so there's no need to expose `RuntimeError::Generic` case)

# Review

- [x] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: Bo Yao <[email protected]>
  • Loading branch information
bors[bot] and ailisp authored Jan 15, 2021
2 parents 4edbde8 + acecc71 commit b4d2d8e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
## **[Unreleased]**

### Added
- [#2026](https://github.com/wasmerio/wasmer/pull/2010) Expose trap code of a `RuntimeError`, if it's a `Trap`.

### Changed

Expand Down
9 changes: 9 additions & 0 deletions lib/engine/src/trap/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,15 @@ impl RuntimeError {
}
}

/// Returns trap code, if it's a Trap
pub fn to_trap(self) -> Option<TrapCode> {
if let RuntimeErrorSource::Trap(trap_code) = self.inner.source {
Some(trap_code)
} else {
None
}
}

/// Returns true if the `RuntimeError` is the same as T
pub fn is<T: Error + 'static>(&self) -> bool {
match &self.inner.source {
Expand Down

0 comments on commit b4d2d8e

Please sign in to comment.