uniffi-rs icon indicating copy to clipboard operation
uniffi-rs copied to clipboard

FR: Optionally disable exception catching in foreign trait code

Open dowski opened this issue 7 months ago • 0 comments

I've created a foreign trait interface that looks like this:

[Trait, WithForeign]
interface QuerySubscriber {
    void on_update(string value);
};

Its purpose is for async events on the Rust side to get propagated to foreign clients. Whenever some new data is available, the Rust code will call the on_update method. That works just fine, but I have an issue with error handling.

On the foreign side (Kotlin/Android in my case) if there's a bug in the onUpdate implementation that triggers an exception, that exception gets handled by UniFFI's generated code[0] and it appears to be sent back to the Rust layer. Since the Rust code is just pushing new values to the foreign side, I'd rather have those exceptions trigger a crash in the foreign code with a nice stack trace rather than get swallowed and an error value get propagated back to the Rust layer.

Does that sound like a reasonable feature request, or is there support for something like this already that I'm just missing?

[0] This is the part of the generated code that catches the exception for sending back to the Rust layer:

internal inline fun<T> uniffiTraitInterfaceCall(
    callStatus: UniffiRustCallStatus,
    makeCall: () -> T,
    writeReturn: (T) -> Unit,
) {
    try {
        writeReturn(makeCall())
    } catch(e: kotlin.Exception) {
        callStatus.code = UNIFFI_CALL_UNEXPECTED_ERROR
        callStatus.error_buf = FfiConverterString.lower(e.toString())
    }
}

dowski avatar Jul 24 '24 17:07 dowski