Skip to content

[14.0.0]: Add an error resource to WASI streams #7169

Merged
alexcrichton merged 1 commit into
bytecodealliance:release-14.0.0from
alexcrichton:backport-error
Oct 6, 2023
Merged

[14.0.0]: Add an error resource to WASI streams #7169
alexcrichton merged 1 commit into
bytecodealliance:release-14.0.0from
alexcrichton:backport-error

Conversation

@alexcrichton
Copy link
Copy Markdown
Member

This is a backport of #7152 to the 14 release branch.

* Change `bindgen!`'s trappable error to take an input type

This commit removes the generated type for `trappable_error_type` from
the `bindgen!` macro to allow users to provide a type instead. This
works similarly as before with new conversion functions generated in
traits which are used to convert the custom error into the ABI
representation of what a WIT world expects.

There are a few motivations for this commit:

* This enables reducing the number of errors in play between async/sync
  bindings by using the same error type there.

* This avoids an auto-generated type which is more difficult to inspect
  than one that's written down already and the source can more easily be
  inspected.

* This enables richer conversions using `self` (e.g. `self.table_mut()`)
  between error types rather than purely relying on `Into`. This is
  important for bytecodealliance#7017 where an error is going to be inserted into the
  table as it gets converted.

* Fix tests

* Update WASI to use new trappable errors

This commit deals with the fallout of the previous commit for the WASI
preview2 implementation. The main changes here are:

* Bindgen-generated `Error` types no longer exist. These are replaced
  with `TrappableError<T>` where type aliases are used such as

  ```rust
  type FsError = TrappableError<wasi::filesystem::types::ErrorCode>;
  ```

* Type synonyms such as `FsResult<T>` are now added for more
  conveniently writing down fallible signatures.

* Some various error conversions are updated from converting to the old
  `Error` type to now instead directly into corresponding `ErrorCode` types.

* A number of cases where unknown errors were turned into traps now
  return bland error codes and log the error instead since these aren't
  fatal events.

* The `StreamError` type does not use `TrappableError` since it has
  other variants that it's concerned with such as a
  `LastOperationFailed` variant which has an `anyhow::Error` payload.

* Some minor preview1 issues were fixed such as trapping errors being
  turned into normal I/O errors by accident.

* Add an `error` resource to WASI streams

This commit adds a new `error` resource to the `wasi:io/streams`
interface. This `error` resource is returned as part of
`last-operation-failed` and serves as a means to discover through other
interfaces more granular type information than a generic string. This
error type has a new function in the `filesystem` interface, for
example, which enables getting filesystem-related error codes from I/O
performed on filesystem-originating streams. This is plumbed through to
the adapter as well to return more than `ERRNO_IO` from failed
read/write operations now too.

This is not super fancy just yet but is intended to be a vector through
which future additions can be done. The main thing this enables is to
avoid dropping errors on the floor in the host and enabling the guest to
discover further information about I/O errors on streams.

Closes bytecodealliance#7017

* Update crates/wasi-http/wit/deps/io/streams.wit

Co-authored-by: Trevor Elliott <awesomelyawesome@gmail.com>

* Update wasi-http wit too

* Remove unnecessary clone

---------

Co-authored-by: Trevor Elliott <awesomelyawesome@gmail.com>
@alexcrichton alexcrichton requested a review from a team as a code owner October 6, 2023 14:00
@alexcrichton alexcrichton requested review from fitzgen and removed request for a team October 6, 2023 14:00
@github-actions github-actions Bot added the wasi Issues pertaining to WASI label Oct 6, 2023
@alexcrichton alexcrichton merged commit 0573117 into bytecodealliance:release-14.0.0 Oct 6, 2023
@alexcrichton alexcrichton deleted the backport-error branch October 6, 2023 16:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wasi Issues pertaining to WASI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants