Skip to content

Commit

Permalink
Use proper error code for input errors (#50)
Browse files Browse the repository at this point in the history
Before this commit, the error was reported with ERRCODE_INTERNAL_ERROR
(SQLSTATE XX000):

```
postgres=# \set VERBOSITY verbose
postgres=# select 'foo'::ulid;
ERROR:  XX000: invalid input syntax for type ulid: "foo": invalid length
LINE 1: select 'foo'::ulid;
               ^
LOCATION:  lib.rs:37
```

With this, ERRCODE_INVALID_TEXT_REPRESENTATION (SQLSTATE 22P02) is used:

```
postgres=# \set VERBOSITY verbose
postgres=# select 'foo'::ulid;
ERROR:  22P02: invalid input syntax for type ulid: "foo": invalid length
LINE 1: select 'foo'::ulid;
               ^
LOCATION:  <ulid::ulid as pgrx::inoutfuncs::InOutFuncs>::input, lib.rs:39
```
  • Loading branch information
hlinnaka authored Aug 20, 2024
1 parent 2709478 commit d05494e
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,16 @@ impl InOutFuncs for ulid {
Self: Sized,
{
let val = input.to_str().unwrap();
let inner = InnerUlid::from_string(val)
.unwrap_or_else(|err| panic!("invalid input syntax for type ulid: \"{val}\": {err}"));

ulid(inner.0)
match InnerUlid::from_string(val) {
Ok(inner) => ulid(inner.0),
Err(err) => {
ereport!(
ERROR,
PgSqlErrorCode::ERRCODE_INVALID_TEXT_REPRESENTATION,
format!("invalid input syntax for type ulid: \"{val}\": {err}")
);
}
}
}

#[inline]
Expand Down

0 comments on commit d05494e

Please sign in to comment.