fix(ext/ffi): improve error messages in FFI module (denoland#16922) #17786
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #16922.
The error messages in the
ffi
module are somewhat cryptic when passing functions that have invalidparameters
orresult
type strings. While the generated serializer for theForeignFunction
struct correctly outputs a correct and verbose message, the user sees a far less helpfuldata did not match any variant
message instead.The underlying cause appears to be the fallback message in the auto-derived deserializer for untagged enums [1] generated as a result of
ForeignSymbol
being marked as#[serde(untagged)]
[2]. Passing an unexpected value forNativeType
causes it to error out while attempting to deserialize both enum variants -- once because it's not a match for theForeignStatic
variant, and once because theForeignFunction
deserializer rejects the invalid type for the parameters/return type. This is currently open as serde #773, and not a trivial exercise to fix generically.[1] https://github.com/serde-rs/serde/blob/v0.9.7/serde_derive/src/de.rs#L730
[2] https://github.com/denoland/deno/blob/main/ext/ffi/dlfcn.rs#L102
[3] serde-rs/serde#773
Note that the auto-generated deserializer for untagged enums uses a private API to buffer deserializer content that we don't have access to. Instead, we can make use of the
serde_value
crate to buffer the values. This can likely be removed once the official buffering API lands (see [4] and [5]). In addition, this crate pulls inserde_json
as a cheap way to test that the deserializer works properly.[4] serde-rs/serde#741
[5] serde-rs/serde#2348