Hi everyone,
what is the best way to handle exceptions for postgresql, for example duplications, ...
i mean howto handle the reason of exception returned from postgresql, how are you guys handling that ?
i'm using honesql + jdbc
@abdullahibra I'm not sure what you're asking. Exceptions come from the PostgreSQL JDBC driver based on problems executing your SQL. They're nothing to do with HoneySQL (or next.jdbc
).
If you're asking about handling constraint violations in SQL, there is syntax in SQL for that (`on duplicate` etc).
If you're using PostgreSQL and you're using HoneySQL, you should also be using the PostgreSQL-specific extensions in https://github.com/nilenso/honeysql-postgres
well, if i try to execute sql statement, and then exception raised i need to define type of exception to create reasonable error message
This shows the tree of exceptions from java.sql.SQLException
on down https://docs.oracle.com/en/java/javase/11/docs/api/java.sql/java/sql/package-tree.html
"duplicates" will mostly be this https://docs.oracle.com/en/java/javase/11/docs/api/java.sql/java/sql/SQLIntegrityConstraintViolationException.html I believe.
Currently what I'm doing is wrapping my query in a try
catch
, and on failure parse the exception.
It's not pretty, but here's the code where I handle a "23505"
exception, which is unique_violation error in postgres: https://www.postgresql.org/docs/9.2/errcodes-appendix.html
https://gist.github.com/kwrooijen/0445ec96295d70f517674ec07281f105
However, I'm not sure if this is a good way to handle this. But it works for me
There's also a :default
key to handle any unknown exceptions, which you can then implement later
That's good
@seancorfield thank you