6

Is an enumeration of the PostgreSQL error codes provided with the JDBC driver, or do we have to create that enumeration ourselves? I can't find anything about the error codes in the JDBC documentation.

BEGIN EDIT

To clarify what I am looking for, this is my existing code:

catch (final SQLException e) {
    if ("23514".equals(e.getSQLState())) {
        // check_violation
    }
}

I feel that most programmers would raise an eyebrow at that line, since it is neither self-documenting nor reusable. Every time a developer needs to determine the cause of the exception, (s)he will be forced to look up the error code online, and hard-code a magic number into the conditional. Instead, I would prefer to write code like:

catch (final SQLException e) {
    if (PGErrors.check_violation.equals(e.getSQLState())) {
        ...
    }
}

Does such a class enumerating the PostgreSQL error codes already exist?

1
  • @ynv check this howto add the link to your comment it seem to be missing now? Since your comment is over 5 mins old you can not edit it. But you can delete it & re-comment. Commented Sep 21, 2018 at 11:38

1 Answer 1

8

Does such a class enumerating the PostgreSQL error codes already exist?

Yes, it does: org.postgresql.util.PSQLState

However, there are 238 error codes listed on the page you referenced, and org.postgresql.util.PSQLState only enumerates 41 values. Of those 41 values, only 33 are from that list of PostgreSQL error codes (roughly 14% coverage).

If you require any of the other constants, you will have to enumerate those yourself.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm not seeing a constant for check_violation in that class. Am I missing something?
It seems list is not complete :( Maybe it's good idea to fill a issue requesting adding missing codes?
Issue #534 (opened March 8, 2016) is to resolve this deficiency in the PostgreSQL JDBC driver. Unfortunately, the comments make it seem that bringing that enumeration in line with the PostgreSQL documentation will be non-trivial. I'll just create my own enumeration until this gets resolved.
I have created a Java Enum with all Postgres 10.0 error codes. Maybe this will save some time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.