honeysql

Discussion of https://github.com/seancorfield/honeysql :slightly_smiling_face:
abdullahibra 2020-07-14T15:20:20.211800Z

Hi everyone,

abdullahibra 2020-07-14T15:21:01.212700Z

what is the best way to handle exceptions for postgresql, for example duplications, ...

abdullahibra 2020-07-14T15:21:35.213400Z

i mean howto handle the reason of exception returned from postgresql, how are you guys handling that ?

abdullahibra 2020-07-14T15:22:41.213800Z

i'm using honesql + jdbc

seancorfield 2020-07-14T15:39:33.214900Z

@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).

seancorfield 2020-07-14T15:40:18.215800Z

If you're asking about handling constraint violations in SQL, there is syntax in SQL for that (`on duplicate` etc).

seancorfield 2020-07-14T15:41:04.216800Z

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

abdullahibra 2020-07-14T15:41:46.217500Z

well, if i try to execute sql statement, and then exception raised i need to define type of exception to create reasonable error message

seancorfield 2020-07-14T15:42:55.218200Z

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

seancorfield 2020-07-14T15:43:28.218700Z

"duplicates" will mostly be this https://docs.oracle.com/en/java/javase/11/docs/api/java.sql/java/sql/SQLIntegrityConstraintViolationException.html I believe.

kwrooijen 2020-07-14T15:52:01.218800Z

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

kwrooijen 2020-07-14T15:54:10.219Z

However, I'm not sure if this is a good way to handle this. But it works for me

kwrooijen 2020-07-14T15:54:47.219200Z

There's also a :default key to handle any unknown exceptions, which you can then implement later

abdullahibra 2020-07-14T16:16:00.219800Z

That's good

abdullahibra 2020-07-14T16:16:06.220Z

@seancorfield thank you