Repository navigation
Add Context to Custom Type Lifting - #2564
Conversation
548cf64 to
e651e9d
Compare
bendk
left a comment
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 })(), |
There was a problem hiding this comment.
Do you need a closure for this? I would have thought #try_lift_expr would work by itself here.
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
Does this test run? I'm not seeing it included in the module tree, but I might just be confused by the trybuild system.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Thanks for the quick and thoughtful response. All the changes seem well motivated to me, let's merge this.
This is based on mozilla/uniffi-rs@3fc88cd. See mozilla/uniffi-rs#2564.
This is based on mozilla/uniffi-rs@3fc88cd. See mozilla/uniffi-rs#2564.
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
anyhow::Context::with_contextto attach information on the conversion being attempted, and where in the codebase it was defined, when thetry_fromcall returns an error.LowerReturndefault and override onResultto format the full anyhow context chain, instead of just the topmost error.