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

Swift binding function throws bufferOverflow when Rust returns a `uniffi::Enum` error

Open crazytonyli opened this issue 7 months ago • 2 comments
trafficstars

I'm not sure it's by design or a bug, but you can return uniffi:Enum as an error in uniffi functions. See the throw_an_enum function below.

When such a function is called from Swift and throws an error, the error returned by the Swift binding is UniffiInternalError.bufferOverflow, which looks like a bug.

#[derive(Debug, Clone, uniffi::Error)]
enum AnError {
    Foo,
    Bar,
}

impl std::fmt::Display for AnError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            AnError::Foo => write!(f, "A error message for Foo"),
            AnError::Bar => write!(f, "A error message for Foo"),
        }
    }
}

#[derive(Debug, Clone, uniffi::Enum)]
enum AnEnum {
    Foo,
    Bar,
}

impl std::fmt::Display for AnEnum {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            AnEnum::Foo => write!(f, "A error message for Foo"),
            AnEnum::Bar => write!(f, "A error message for Foo"),
        }
    }
}

#[uniffi::export]
fn throw_an_error() -> Result<bool, AnError> {
    Err(AnError::Foo)
}

#[uniffi::export]
fn throw_an_enum() -> Result<bool, AnEnum> {
    Err(AnEnum::Foo)
}
@Test
func testThrow() throws {
    do {
        let _ = try throwAnError()
    } catch {
        // Output: WordPressAPIInternal.AnError.Foo
        debugPrint(error)
    }
    do {
        let _ = try throwAnEnum()
    } catch {
        // Output: WordPressAPIInternal.(unknown context at $108fd5da8).UniffiInternalError.bufferOverflow
        debugPrint(error)
    }
}

crazytonyli avatar Mar 26 '25 00:03 crazytonyli