Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL JDBC Error Code Enumeration

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?

like image 664
Jeff G Avatar asked Oct 28 '16 19:10

Jeff G


1 Answers

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.

like image 145
Radek Postołowicz Avatar answered Oct 07 '22 05:10

Radek Postołowicz