Skip to content

Conversation

@eeeebbbbrrrr
Copy link
Contributor

@eeeebbbbrrrr eeeebbbbrrrr commented Dec 20, 2022

This pulls out some commits from #969 into its own PR to add support for #[pg_extern]-style functions returning a Result of some kind.

We don't care about the type of the Error, so long as it implements Display. If the returned value is the Err variant, the IntoDatum impl for Result<T, E> will raise a Postgres ERROR using ereport!(ERROR, PgSqlErrorCode::ERRCODE_DATA_EXCEPTION, &format!("{}", e)).

It's tested to also work with the eyre::Result type along with returning Result<Option<T>, E>.

It also allows #[pg_test] functions to return Results which makes writing unit tests a little nicer.

…s a `Result`. Requires special handling for `Result<Option<T>, E>`.
…<T, E>` such that `E` only needs to be `Display` instead of an actual `std::error::Error`.

This lets functions use `eyre::Result`.  For example:

```rust
fn read_file(path: &str) -> eyre::Result<Vec<u8>> {
    Ok(std::fs::read(path)?)
}
```
…any()` is not the right way to get the error visible to the user.
…ise the error as it uses the correct `PgSqlErrorCode`
Copy link
Contributor Author

@eeeebbbbrrrr eeeebbbbrrrr left a comment

Choose a reason for hiding this comment

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

I approve of my own changes. I can't see any more tests that need to be added, and everything passes.

This will also cleanup the changeset in #969.

@eeeebbbbrrrr eeeebbbbrrrr merged commit c4c9c32 into develop Dec 20, 2022
@eeeebbbbrrrr eeeebbbbrrrr deleted the result-into-datum branch June 20, 2023 18:00
usamoi pushed a commit to tensorchord/pgrx that referenced this pull request Mar 6, 2025
…ation#972)

This pulls out some commits from pgcentralfoundation#969 into its own PR to add support for `#[pg_extern]`-style functions returning a `Result` of some kind.

We don't care about the type of the Error, so long as it implements `Display`.  If the returned value is the Err variant, the `IntoDatum` impl for `Result<T, E>` will raise a Postgres ERROR using `ereport!(ERROR, PgSqlErrorCode::ERRCODE_DATA_EXCEPTION, &format!("{}", e))`.

It's tested to also work with the [`eyre::Result`](https://docs.rs/eyre/latest/eyre/type.Result.html) type along with returning `Result<Option<T>, E>`.

The case of returning a `Result<(), E>` uncovered some issues with how pgx converts `()` into a Datum, so those have been fixed along the way.

It also allows `#[pg_test]` functions to return Results which makes writing unit tests a little nicer.
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