Skip to content

Add Context to Custom Type Lifting - #2564

Merged
bendk merged 9 commits into
mozilla:mainfrom
oldwomanjosiah:josiah/error_annotations
Jun 9, 2025
Merged

bendk merged 9 commits into
mozilla:mainfrom
oldwomanjosiah:josiah/error_annotations

Conversation

@oldwomanjosiah

@oldwomanjosiah oldwomanjosiah commented Jun 7, 2025 •

Copy link
Copy Markdown
Contributor

Relates to #2563

Motivation

In many cases it is helpful to use the custom_type!() macro to delegate the conversion between domain and FFI-types, but this means that users of the library do not have direct control over the error messages which are produced in cases where the conversion fails. These are currently surfaced by the host language as an "internal error", which can lead to confusion when debugging.

Proposed Change

  • Use anyhow::Context::with_context to attach information on the conversion being attempted, and where in the codebase it was defined, when the try_from call returns an error.
  • Update LowerReturn default and override on Result to format the full anyhow context chain, instead of just the topmost error.

@oldwomanjosiah
oldwomanjosiah requested a review from a team as a code owner June 7, 2025 06:08
@oldwomanjosiah
oldwomanjosiah requested review from bendk and removed request for a team June 7, 2025 06:08
@oldwomanjosiah
oldwomanjosiah marked this pull request as draft June 7, 2025 18:56
@oldwomanjosiah
oldwomanjosiah force-pushed the josiah/error_annotations branch from 548cf64 to e651e9d Compare June 7, 2025 20:34
@oldwomanjosiah
oldwomanjosiah marked this pull request as ready for review June 7, 2025 21:24

@bendk bendk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Overall this is nice. I don't think there's a big performance penalty since custom type conversion errors should be rare. I just had some minor suggestions.

Err(error) => crate::LiftArgsError { arg_name, error }.to_internal_error(),
};

Err(error)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does this change the behavior or is it just a refactoring? I kind of prefer the original version, since it keeps all the logic in one place. What's the motivation to change this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The behavior here is the same, but I pulled out the formatting out so that we could use the same function for the default impl and this override.

It's not enough code that that feels super necessary, but the duplication tripped me up while I was working on this so I figured I'd centralize.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh, I missed that this removed some duplication. It all makes sense to me now.


def get_value_arg_issue(value):
return f"""Failed to convert arg 'value':
Lifting custom type `ext_types_custom::Guid` from FFI type `alloc::string::String` failed at fixtures/ext-types/custom-types/src/lib.rs:101

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this enough context to understand the error? Pointing to the custom_type! macro is feels like an improvement, but maybe a full backtrace would be even better. That's probably more expensive though, so maybe there should be a feature flag for it.

I'm mostly just thinking out loud. If this string feels good to you, then I'm happy to go with it now and and we can continue to iterate on it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not dead set on this specific message, but I do think that adding in the attempted conversion and source location is enough to catch most issues that a user would run into. I'm not entirely sure when anyhow will choose to print out a backtrace, but I do know that it should be propagating it if it is captured by the source error.

I originally had a version that was more explicit about the fact that we had delegated to try_from, but it felt both too verbose and also potentially incorrect when the end user provided some custom conversion code.

#[allow(clippy::needless_question_mark)]
{
::uniffi::deps::anyhow::Context::with_context(
(move || -> ::uniffi::Result<#custom_type> { #try_lift_expr })(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do you need a closure for this? I would have thought #try_lift_expr would work by itself here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

A closure is needed here to catch errors propagated with ? or return in the user provided code

uniffi::setup_scaffolding!();

#[test]
fn test_string_validation_failure() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does this test run? I'm not seeing it included in the module tree, but I might just be confused by the trybuild system.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, rust files in the <crate root>/tests directory are automatically included as "integration tests". Originally, I placed this here as it used cargo-insta to validate the error message and it felt cleaner to have the snapshots out of the main src tree, but the dependency managment docs are missing and I didn't want to add it without being able to review the rules there.

I can move these back into src/lib.rs::tests if you would prefer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Okay actually, I just attempted to move the test as I mentioned, and all the macros assume that you will be able to import with the ::uniffi syntax, which requires that uniffi be a crate dependency. This is true for integration tests, but not unit tests in the same crate

@bendk bendk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the quick and thoughtful response. All the changes seem well motivated to me, let's merge this.

@bendk
bendk merged commit 6cfc43b into mozilla:main Jun 9, 2025
istankovic added a commit to wireapp/uniffi-bindgen-react-native that referenced this pull request Dec 15, 2025
istankovic added a commit to wireapp/uniffi-bindgen-react-native that referenced this pull request Dec 22, 2025
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.

2 participants