pchickey opened PR #1790 from pch/wiggle_error_transforms to master:
One issue we've had with wiggle ergonomics is that we often want to use a rich error type (a Rust enum that uses
thiserror::Error, or maybe ananyhow::Error) to write the library code that ends up in an impl of the wiggle-defined module traits. However, those module traits require an error type that corresponds to the witx document.Because Rust doesn't have
try, there's not a lot of good mechanical ways to automatically replace all uses of a witx document error type like wasi'sErrnowith a rich type and a conversion function.
So, this PR adds a totally opt-in facility to wiggle to perform such a conversion on the user's behalf.The new test
src/wiggle/tests/errors.rsshows how the types work out on a modified version of thearraytest. A new optional wiggle proc-macro argumenterrorsgives a mapping of witx identifiers (less the leading$, because proc macro parsing is enough trouble without that) to the user's desired error type, e.g.:wiggle::from_witx!({ witx: ["tests/arrays.witx"], ctx: WasiCtx, errors: { errno => RichError }, });When the
errorsconfig is provided, aUserErrorConversiontrait is defined intypesthat the user must implement for their ctx type. The method names in this enum are determined mechanically.impl<'a> types::UserErrorConversion for WasiCtx<'a> { fn errno_from_rich_error(&self, _e: RichError) -> types::Errno { unimplemented!(); } }The
RichErrortype is then used in place oftypes::Errnoin the witx module traits:impl<'a> arrays::Arrays for WasiCtx<'a> { fn populate_excuses(&self, _excuses: &types::ExcuseArray) -> Result<(), RichError> { unimplemented!() } (snip) }Open questions:
- The syntax of the arguments is pretty arbitrary. I wanted to permit full paths to the user's rich error type, but then I ran into a problem figuring out a sensible and unique method name from that, so I punted. Ideally we'd add optional syntax for specifying the method name.
- I wasn't always consistent about what to call this feature in the code - is this an ErrorTransform, is this a UserError? The naming could stand some bikeshedding.
<!--
Please ensure that the following steps are all taken care of before submitting
the PR.
[ ] This has been discussed in issue #..., or if not, please tell us why
here.[ ] A short description of what this does, why it is needed; if the
description becomes long, the matter should probably be discussed in an issue
first.[ ] This PR contains test cases, if meaningful.
- [ ] A reviewer from the core maintainer team has been assigned for this PR.
If you don't know who could review this, please indicate so. The list of
suggested reviewers on the right can help you.Please ensure all communication adheres to the code of conduct.
-->
pchickey requested alexcrichton and kubkon for a review on PR #1790.
pchickey requested alexcrichton and kubkon for a review on PR #1790.
pchickey edited PR #1790 from pch/wiggle_error_transforms to master:
One issue we've had with wiggle ergonomics is that we often want to use a rich error type (a Rust enum that uses
thiserror::Error, or maybe ananyhow::Error) to write the library code that ends up in an impl of the wiggle-defined module traits. However, those module traits require an error type that corresponds to the witx document.Because Rust doesn't have
try, there's not a lot of good mechanical ways to automatically replace all uses of a witx document error type like wasi'sErrnowith a rich type and a conversion function.
So, this PR adds a totally opt-in facility to wiggle to perform such a conversion on the user's behalf.The new test
src/wiggle/tests/errors.rsshows how the types work out on a modified version of thearraytest. A new optional wiggle proc-macro argumenterrorsgives a mapping of witx identifiers (less the leading$, because proc macro parsing is enough trouble without that) to the user's desired error type, e.g.:wiggle::from_witx!({ witx: ["tests/arrays.witx"], ctx: WasiCtx, errors: { errno => RichError }, });When the
errorsconfig is provided, aUserErrorConversiontrait is defined intypesthat the user must implement for their ctx type. The method names in this enum are determined mechanically.impl<'a> types::UserErrorConversion for WasiCtx<'a> { fn errno_from_rich_error(&self, _e: RichError) -> types::Errno { unimplemented!(); } }The
RichErrortype is then used in place oftypes::Errnoin the witx module traits:impl<'a> arrays::Arrays for WasiCtx<'a> { fn populate_excuses(&self, _excuses: &types::ExcuseArray) -> Result<(), RichError> { unimplemented!() } (snip) }Open questions:
- The syntax of the arguments is pretty arbitrary. I wanted to permit full paths to the user's rich error type, but then I ran into a problem figuring out a sensible and unique method name from that, so I punted. Ideally we'd add optional syntax for specifying the method name, and require the syntax if a sensible name cant be determined by some simple check (currently https://docs.rs/syn/1.0.28/syn/struct.Path.html#method.get_ident)
- I wasn't always consistent about what to call this feature in the code - is this an ErrorTransform, is this a UserError? The naming could stand some bikeshedding.
<!--
Please ensure that the following steps are all taken care of before submitting
the PR.
[ ] This has been discussed in issue #..., or if not, please tell us why
here.[ ] A short description of what this does, why it is needed; if the
description becomes long, the matter should probably be discussed in an issue
first.[ ] This PR contains test cases, if meaningful.
- [ ] A reviewer from the core maintainer team has been assigned for this PR.
If you don't know who could review this, please indicate so. The list of
suggested reviewers on the right can help you.Please ensure all communication adheres to the code of conduct.
-->
pchickey edited PR #1790 from pch/wiggle_error_transforms to master:
One issue we've had with wiggle ergonomics is that we often want to use a rich error type (a Rust enum that uses
thiserror::Error, or maybe ananyhow::Error) to write the library code that ends up in an impl of the wiggle-defined module traits. However, those module traits require an error type that corresponds to the witx document.Because Rust doesn't have
try, there's not a lot of good mechanical ways to automatically replace all uses of a witx document error type like wasi'sErrnowith a rich type and a conversion function.
So, this PR adds a totally opt-in facility to wiggle to perform such a conversion on the user's behalf.The new test
src/wiggle/tests/errors.rsshows how the types work out on a modified version of thearraytest. A new optional wiggle proc-macro argumenterrorsgives a mapping of witx identifiers (less the leading$, because proc macro parsing is enough trouble without that) to the user's desired error type, e.g.:pub enum RichError { (snip) } wiggle::from_witx!({ witx: ["tests/arrays.witx"], ctx: WasiCtx, errors: { errno => RichError }, });When the
errorsconfig is provided, aUserErrorConversiontrait is defined intypesthat the user must implement for their ctx type. The method names in this enum are determined mechanically.impl<'a> types::UserErrorConversion for WasiCtx<'a> { fn errno_from_rich_error(&self, _e: RichError) -> types::Errno { unimplemented!(); } }The
RichErrortype is then used in place oftypes::Errnoin the witx module traits:impl<'a> arrays::Arrays for WasiCtx<'a> { fn populate_excuses(&self, _excuses: &types::ExcuseArray) -> Result<(), RichError> { unimplemented!() } (snip) }Open questions:
- The syntax of the arguments is pretty arbitrary. I wanted to permit full paths to the user's rich error type, but then I ran into a problem figuring out a sensible and unique method name from that, so I punted. Ideally we'd add optional syntax for specifying the method name, and require the syntax if a sensible name cant be determined by some simple check (currently https://docs.rs/syn/1.0.28/syn/struct.Path.html#method.get_ident)
- I wasn't always consistent about what to call this feature in the code - is this an ErrorTransform, is this a UserError? The naming could stand some bikeshedding.
<!--
Please ensure that the following steps are all taken care of before submitting
the PR.
[ ] This has been discussed in issue #..., or if not, please tell us why
here.[ ] A short description of what this does, why it is needed; if the
description becomes long, the matter should probably be discussed in an issue
first.[ ] This PR contains test cases, if meaningful.
- [ ] A reviewer from the core maintainer team has been assigned for this PR.
If you don't know who could review this, please indicate so. The list of
suggested reviewers on the right can help you.Please ensure all communication adheres to the code of conduct.
-->
alexcrichton submitted PR Review.
kubkon submitted PR Review.
pchickey updated PR #1790 from pch/wiggle_error_transforms to master:
One issue we've had with wiggle ergonomics is that we often want to use a rich error type (a Rust enum that uses
thiserror::Error, or maybe ananyhow::Error) to write the library code that ends up in an impl of the wiggle-defined module traits. However, those module traits require an error type that corresponds to the witx document.Because Rust doesn't have
try, there's not a lot of good mechanical ways to automatically replace all uses of a witx document error type like wasi'sErrnowith a rich type and a conversion function.
So, this PR adds a totally opt-in facility to wiggle to perform such a conversion on the user's behalf.The new test
src/wiggle/tests/errors.rsshows how the types work out on a modified version of thearraytest. A new optional wiggle proc-macro argumenterrorsgives a mapping of witx identifiers (less the leading$, because proc macro parsing is enough trouble without that) to the user's desired error type, e.g.:pub enum RichError { (snip) } wiggle::from_witx!({ witx: ["tests/arrays.witx"], ctx: WasiCtx, errors: { errno => RichError }, });When the
errorsconfig is provided, aUserErrorConversiontrait is defined intypesthat the user must implement for their ctx type. The method names in this enum are determined mechanically.impl<'a> types::UserErrorConversion for WasiCtx<'a> { fn errno_from_rich_error(&self, _e: RichError) -> types::Errno { unimplemented!(); } }The
RichErrortype is then used in place oftypes::Errnoin the witx module traits:impl<'a> arrays::Arrays for WasiCtx<'a> { fn populate_excuses(&self, _excuses: &types::ExcuseArray) -> Result<(), RichError> { unimplemented!() } (snip) }Open questions:
- The syntax of the arguments is pretty arbitrary. I wanted to permit full paths to the user's rich error type, but then I ran into a problem figuring out a sensible and unique method name from that, so I punted. Ideally we'd add optional syntax for specifying the method name, and require the syntax if a sensible name cant be determined by some simple check (currently https://docs.rs/syn/1.0.28/syn/struct.Path.html#method.get_ident)
- I wasn't always consistent about what to call this feature in the code - is this an ErrorTransform, is this a UserError? The naming could stand some bikeshedding.
<!--
Please ensure that the following steps are all taken care of before submitting
the PR.
[ ] This has been discussed in issue #..., or if not, please tell us why
here.[ ] A short description of what this does, why it is needed; if the
description becomes long, the matter should probably be discussed in an issue
first.[ ] This PR contains test cases, if meaningful.
- [ ] A reviewer from the core maintainer team has been assigned for this PR.
If you don't know who could review this, please indicate so. The list of
suggested reviewers on the right can help you.Please ensure all communication adheres to the code of conduct.
-->
pchickey requested alexcrichton for a review on PR #1790.
alexcrichton submitted PR Review.
pchickey merged PR #1790.
Last updated: Dec 13 2025 at 19:03 UTC