Postgresql Error Codes Java
Contents |
8.3 / 8.4 / 9.0 PostgreSQL 9.4.9 Documentation Prev Up Next Appendix A. PostgreSQL Error Codes All messages emitted by the PostgreSQL server are assigned five-character error codes that follow postgres error codes the SQL standard's conventions for "SQLSTATE" codes. Applications that need to know which error
Psqlexception
condition has occurred should usually test the error code, rather than looking at the textual error message. The error codes are less likely to change across PostgreSQL releases, and also are not subject to change due to localization of error messages. Note that some, but not all, of the error codes produced by PostgreSQL are defined by the SQL standard; some additional error codes for conditions not defined by the standard have been invented or borrowed from other databases. According to the standard, the first two characters of an error code denote a class of errors, while the last three characters indicate a specific condition within that class. Thus, an application that does not recognize the specific error code might still be able to infer what to do from the error class. Table A-1 lists all the error codes defined in PostgreSQL 9.4.9. (Some are not actually used at present, but are defined by the SQL standard.) The error classes are also shown. For each error class there is a "standard" error code having the last three characters 000. This code is used only for error conditions that fall within the class but do not have any more-specific code assigned. The symbol shown in the column "Condition Name" is the condition name to use in PL/pgSQL. Condition names can be written in either upper or lower case. (Note that PL/pgSQL does not recognize warning, as opposed to error, condition names; those are classes 00, 01, and 02.) For some types of errors, the server reports the name of a database object (a table, table column, data type, or constraint) associated with the error; for example, the name of the unique constraint that caused a unique_violation error. Such names are supplied in separate fields of the error report message so that applications need not try to extract them from the possibly-localized human-readable text of the message. As of PostgreSQL 9.3, complete coverage for this feature exists only for errors in SQLSTATE class 23 (integrity constraint violation), but this is like
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, https://www.postgresql.org/docs/9.4/static/errcodes-appendix.html helping each other. Join them; it only takes a minute: Sign up Postgres JDBC: Especific error code of PSQLException? up vote 0 down vote favorite When writing java code that uses an Oracle database, one can always catch SQLException an read an specific Oracle error with e.getErrorCode(). For example, error 28001 means expired password, 28000 is blocked account, http://stackoverflow.com/questions/28416445/postgres-jdbc-especific-error-code-of-psqlexception 1017 is wrong user/passsword, etc. That way I can manage different errors the appropiate way. But with PostgreSQL databases e.getErrorCode() always returns 0, even when catching Postgres-specific PSQLException. The Question Is there a way that I don't know of to get an specific error code for a Postgres database exception in Java other than trying to parse the error message (which by the way could be in any localized language)? java postgresql exception jdbc error-handling share|improve this question asked Feb 9 '15 at 18:10 Tulains Córdova 1,3951721 1 Have you tried looking at getSqlState() instead? See also: postgresql.org/docs/9.3/static/errcodes-appendix.html –cjstehno Feb 9 '15 at 18:28 Just discovered that, please write an answer to accept it. –Tulains Córdova Feb 9 '15 at 18:31 add a comment| 1 Answer 1 active oldest votes up vote 0 down vote accepted Have you tried looking at getSqlState() instead? See also: http://www.postgresql.org/docs/9.3/static/errcodes-appendix.html share|improve this answer answered Feb 9 '15 at 18:32 cjstehno 5,80022042 Every failed connection attempt returns "08001" regardless o
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies http://stackoverflow.com/questions/446663/best-way-to-define-error-codes-strings-in-java of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a error codes minute: Sign up Best way to define error codes/strings in Java? up vote 75 down vote favorite 47 I am writing a web service in Java, and I am trying to figure out the best way to define error codes and their associated error strings. I need to have a numerical error code and an error string grouped together. Both the error postgresql error codes code and error string will be sent to the client accessing the web service. For example, when a SQLException occurs, I might want to do the following: // Example: errorCode = 1, // errorString = "There was a problem accessing the database." throw new SomeWebServiceException(errorCode, errorString); The client program might be shown the message: "Error #1 has occured: There was a problem accessing the database." My first thought was to used an Enum of the error codes and override the toString methods to return the error strings. Here is what I came up with: public enum Errors { DATABASE { @Override public String toString() { return "A database error has occured."; } }, DUPLICATE_USER { @Override public String toString() { return "This user already exists."; } }, // more errors follow } My question is: Is there a better way to do this? I would prefer an solution in code, rather than reading from an external file. I am using Javadoc for this project, and being able to document the error codes in-line and have them automatically update in the documentation would be h