Exposed icon indicating copy to clipboard operation
Exposed copied to clipboard

Exception is printed even when exception is caught

Open IP14Y3RI opened this issue 5 years ago • 12 comments

So I have a table called User, with it's name column having an UNIQUE-constraint in the database, which defined in the model using .uniqueIndex() method. You would expect that when I insert an user with a name which already exists like this:

addedUser = transaction {
                createdUser = transaction {
                User {
                    name = "Someone"
                    age = 9
                }

            }

, this would result in an exception being raised, like this:

org.jetbrains.exposed.exceptions.ExposedSQLException: java.sql.BatchUpdateException: Duplicate entry 'Someone' for key 'Name_UNIQUE'

But, when I put a try-catch clause around it like this:

try {
         createdUser = transaction {
                User {
                    name = "Someone"
                    age = 9
                }
            }
        }catch (ex : ExposedSQLException){ //Or any other exception instead of ExcposedSQLException, even ex : Exception
            println("Caught.")
        }

, it still prints out this:

org.jetbrains.exposed.exceptions.ExposedSQLException: java.sql.BatchUpdateException: Duplicate entry 'Someone' for key 'Name_UNIQUE'

My question is: Why does it still print all this rubbish in the console, even though the exception is caught? When I debug and place an breakpoint on the catch clause, the code stops at the breakpoint, which implies the exception has been caught. Still, it prints the whole stacktrace.

I have a feeling it is due to the fact the code snippet which inserts into the database is being invoked by a suspend fun someFuntion , but I am not sure.

IP14Y3RI avatar Apr 15 '19 12:04 IP14Y3RI

Exception is thrown within transaction{} block and logs there. If you want to catch an ExposedSQLException before it logged to exposedLogger you have to try/catch within transacton.

createdUser = transaction {
    try {
        User {
            name = "Someone"
            age = 9
        }
    } catch (ex : ExposedSQLException) { 
            println("Caught.")
    }
}

Tapac avatar Apr 15 '19 15:04 Tapac

That does not seem to work, unfortunately... The code you provided

var createdUser: User? = null
        createdUser = transaction {
            try {
                 User.new {
                    name = "Someone"
                    age = 9
                }
            }catch (e : ExposedSQLException){
                println("_______Caught.")
            }
        }

results in a failure in inferencing type due to a type mismatch: createdUser requires to be of type User?, not of type Any, which it is returning now.

However, if I do this instead:

var createdUser: User? = null

        transaction {
            try {
                createdUser = User.new {
                    name = "Someone"
                    age = 9
                }
            }catch (e : ExposedSQLException){
                println("_______Caught.")
            }
        }

, then the typemismatch error is solved, but now the exception is never caught, and the stacktrace is still printed.

IP14Y3RI avatar Apr 16 '19 09:04 IP14Y3RI

I am also seeing this issue as well when trying to catch exceptions raised from inside a transaction. It seems that I am unable to prevent the error from surfacing regardless of whether I catch inside or outside the transaction block.

Executing the below results in the following logs. Both catch blocks are executed and it appears impossible to prevent the error from surfacing.

	try {
	    transaction {
	        try {
		    exec("CREATE TYPE ParcelStatusEnum AS ENUM ('Pending', 'Success', 'Fail');")
		} catch(e: Throwable) {
		    println("inside tx")
		}
	    }
	} catch(e: Throwable) {
	    println("outside tx")
	    println("ParcelStatusEnum already exists, skipping...")
	}
 :: Spring Boot ::        (v2.2.6.RELEASE)

2020-04-02 09:37:03.337  INFO 61961 --- [           main] c.i.cocsandbox.CocSandboxApplicationKt   : Starting CocSandboxApplicationKt on rvinzent-REDACTED with PID 61961 (/Users/rvinzent/REDACTED/build/classes/kotlin/main started by rvinzent in /Users/rvinzent/REDACTED)
2020-04-02 09:37:03.339  INFO 61961 --- [           main] c.i.cocsandbox.CocSandboxApplicationKt   : No active profile set, falling back to default profiles: default
2020-04-02 09:37:03.809  INFO 61961 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JDBC repositories in DEFAULT mode.
2020-04-02 09:37:03.827  INFO 61961 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 14ms. Found 0 JDBC repository interfaces.
2020-04-02 09:37:04.139  INFO 61961 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-04-02 09:37:04.146  INFO 61961 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-04-02 09:37:04.147  INFO 61961 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.33]
2020-04-02 09:37:04.223  INFO 61961 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-04-02 09:37:04.223  INFO 61961 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 850 ms
2020-04-02 09:37:04.409  INFO 61961 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-04-02 09:37:04.606  INFO 61961 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-04-02 09:37:04.610  INFO 61961 --- [           main] c.i.cocsandbox.CocSandboxApplicationKt   : Started CocSandboxApplicationKt in 1.555 seconds (JVM running for 1.922)
2020-04-02 09:37:04.613  INFO 61961 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-04-02 09:37:04.683  INFO 61961 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
inside tx
2020-04-02 09:37:04.736  WARN 61961 --- [           main] Exposed                                  : Transaction attempt #0 failed: The database returned ROLLBACK, so the transaction cannot be committed. Transaction failure cause is <<ERROR: type "parcelstatusenum" already exists>>. Statement(s): org.jetbrains.exposed.sql.statements.jdbc.JdbcPreparedStatementImpl@7bc6935c

org.postgresql.util.PSQLException: The database returned ROLLBACK, so the transaction cannot be committed. Transaction failure cause is <<ERROR: type "parcelstatusenum" already exists>>
	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2209) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:331) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.jdbc.PgConnection.executeTransactionCommand(PgConnection.java:833) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.jdbc.PgConnection.commit(PgConnection.java:855) ~[postgresql-42.2.11.jar:42.2.11]
	at com.zaxxer.hikari.pool.ProxyConnection.commit(ProxyConnection.java:366) ~[HikariCP-3.4.2.jar:na]
	at com.zaxxer.hikari.pool.HikariProxyConnection.commit(HikariProxyConnection.java) ~[HikariCP-3.4.2.jar:na]
	at org.jetbrains.exposed.sql.statements.jdbc.JdbcConnectionImpl.commit(JdbcConnectionImpl.kt:27) ~[exposed-jdbc-0.23.1.jar:na]
	at org.jetbrains.exposed.spring.SpringTransactionManager$SpringTransaction.commit(SpringTransactionManager.kt:96) ~[spring-transaction-0.22.1.jar:na]
	at org.jetbrains.exposed.sql.Transaction.commit(Transaction.kt:62) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction$1.invoke(ThreadLocalTransactionManager.kt:157) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction$2.invoke(ThreadLocalTransactionManager.kt:197) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.keepAndRestoreTransactionRefAfterRun(ThreadLocalTransactionManager.kt:205) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.inTopLevelTransaction(ThreadLocalTransactionManager.kt:196) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$transaction$1.invoke(ThreadLocalTransactionManager.kt:134) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.keepAndRestoreTransactionRefAfterRun(ThreadLocalTransactionManager.kt:205) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:106) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:104) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction$default(ThreadLocalTransactionManager.kt:103) ~[exposed-core-0.23.1.jar:na]
	at com.invitae.cocsandbox.CocSandboxApplicationKt.main(CocSandboxApplication.kt:29) ~[main/:na]
Caused by: org.postgresql.util.PSQLException: ERROR: type "parcelstatusenum" already exists
	at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2578) ~[postgresql-42.2.11.jar:42.2.11]
Caused by: org.postgresql.util.PSQLException: ERROR: type "parcelstatusenum" already exists

	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2313) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:331) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:448) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:369) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:159) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:125) ~[postgresql-42.2.11.jar:42.2.11]
	at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~[HikariCP-3.4.2.jar:na]
	at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-3.4.2.jar:na]
	at org.jetbrains.exposed.sql.statements.jdbc.JdbcPreparedStatementImpl.executeUpdate(JdbcPreparedStatementImpl.kt:23) ~[exposed-jdbc-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.Transaction$exec$2.executeInternal(Transaction.kt:93) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.statements.Statement.executeIn$exposed_core(Statement.kt:61) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:126) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:112) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:88) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:79) ~[exposed-core-0.23.1.jar:na]
	at com.invitae.cocsandbox.CocSandboxApplicationKt$main$1.invoke(CocSandboxApplication.kt:31) ~[main/:na]
	at com.invitae.cocsandbox.CocSandboxApplicationKt$main$1.invoke(CocSandboxApplication.kt) ~[main/:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction$1.invoke(ThreadLocalTransactionManager.kt:156) ~[exposed-core-0.23.1.jar:na]
	... 9 common frames omitted

inside tx
2020-04-02 09:37:04.752  WARN 61961 --- [           main] Exposed                                  : Transaction attempt #1 failed: The database returned ROLLBACK, so the transaction cannot be committed. Transaction failure cause is <<ERROR: type "parcelstatusenum" already exists>>. Statement(s): org.jetbrains.exposed.sql.statements.jdbc.JdbcPreparedStatementImpl@77429040

org.postgresql.util.PSQLException: The database returned ROLLBACK, so the transaction cannot be committed. Transaction failure cause is <<ERROR: type "parcelstatusenum" already exists>>
	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2209) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:331) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.jdbc.PgConnection.executeTransactionCommand(PgConnection.java:833) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.jdbc.PgConnection.commit(PgConnection.java:855) ~[postgresql-42.2.11.jar:42.2.11]
	at com.zaxxer.hikari.pool.ProxyConnection.commit(ProxyConnection.java:366) ~[HikariCP-3.4.2.jar:na]
	at com.zaxxer.hikari.pool.HikariProxyConnection.commit(HikariProxyConnection.java) ~[HikariCP-3.4.2.jar:na]
	at org.jetbrains.exposed.sql.statements.jdbc.JdbcConnectionImpl.commit(JdbcConnectionImpl.kt:27) ~[exposed-jdbc-0.23.1.jar:na]
	at org.jetbrains.exposed.spring.SpringTransactionManager$SpringTransaction.commit(SpringTransactionManager.kt:96) ~[spring-transaction-0.22.1.jar:na]
	at org.jetbrains.exposed.sql.Transaction.commit(Transaction.kt:62) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction$1.invoke(ThreadLocalTransactionManager.kt:157) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction$2.invoke(ThreadLocalTransactionManager.kt:197) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.keepAndRestoreTransactionRefAfterRun(ThreadLocalTransactionManager.kt:205) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.inTopLevelTransaction(ThreadLocalTransactionManager.kt:196) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$transaction$1.invoke(ThreadLocalTransactionManager.kt:134) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.keepAndRestoreTransactionRefAfterRun(ThreadLocalTransactionManager.kt:205) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:106) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:104) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction$default(ThreadLocalTransactionManager.kt:103) ~[exposed-core-0.23.1.jar:na]
	at com.invitae.cocsandbox.CocSandboxApplicationKt.main(CocSandboxApplication.kt:29) ~[main/:na]
Caused by: org.postgresql.util.PSQLException: ERROR: type "parcelstatusenum" already exists
	at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2578) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2313) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:331) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:448) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:369) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:159) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:125) ~[postgresql-42.2.11.jar:42.2.11]
	at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~[HikariCP-3.4.2.jar:na]
	at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-3.4.2.jar:na]
	at org.jetbrains.exposed.sql.statements.jdbc.JdbcPreparedStatementImpl.executeUpdate(JdbcPreparedStatementImpl.kt:23) ~[exposed-jdbc-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.Transaction$exec$2.executeInternal(Transaction.kt:93) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.statements.Statement.executeIn$exposed_core(Statement.kt:61) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:126) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:112) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:88) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:79) ~[exposed-core-0.23.1.jar:na]
	at com.invitae.cocsandbox.CocSandboxApplicationKt$main$1.invoke(CocSandboxApplication.kt:31) ~[main/:na]
	at com.invitae.cocsandbox.CocSandboxApplicationKt$main$1.invoke(CocSandboxApplication.kt) ~[main/:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction$1.invoke(ThreadLocalTransactionManager.kt:156) ~[exposed-core-0.23.1.jar:na]
	... 9 common frames omitted

inside tx
2020-04-02 09:37:04.767  WARN 61961 --- [           main] Exposed                                  : Transaction attempt #2 failed: The database returned ROLLBACK, so the transaction cannot be committed. Transaction failure cause is <<ERROR: type "parcelstatusenum" already exists>>. Statement(s): org.jetbrains.exposed.sql.statements.jdbc.JdbcPreparedStatementImpl@38291795

org.postgresql.util.PSQLException: The database returned ROLLBACK, so the transaction cannot be committed. Transaction failure cause is <<ERROR: type "parcelstatusenum" already exists>>
	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2209) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:331) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.jdbc.PgConnection.executeTransactionCommand(PgConnection.java:833) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.jdbc.PgConnection.commit(PgConnection.java:855) ~[postgresql-42.2.11.jar:42.2.11]
	at com.zaxxer.hikari.pool.ProxyConnection.commit(ProxyConnection.java:366) ~[HikariCP-3.4.2.jar:na]
	at com.zaxxer.hikari.pool.HikariProxyConnection.commit(HikariProxyConnection.java) ~[HikariCP-3.4.2.jar:na]
	at org.jetbrains.exposed.sql.statements.jdbc.JdbcConnectionImpl.commit(JdbcConnectionImpl.kt:27) ~[exposed-jdbc-0.23.1.jar:na]
	at org.jetbrains.exposed.spring.SpringTransactionManager$SpringTransaction.commit(SpringTransactionManager.kt:96) ~[spring-transaction-0.22.1.jar:na]
	at org.jetbrains.exposed.sql.Transaction.commit(Transaction.kt:62) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction$1.invoke(ThreadLocalTransactionManager.kt:157) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction$2.invoke(ThreadLocalTransactionManager.kt:197) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.keepAndRestoreTransactionRefAfterRun(ThreadLocalTransactionManager.kt:205) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.inTopLevelTransaction(ThreadLocalTransactionManager.kt:196) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$transaction$1.invoke(ThreadLocalTransactionManager.kt:134) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.keepAndRestoreTransactionRefAfterRun(ThreadLocalTransactionManager.kt:205) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:106) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:104) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction$default(ThreadLocalTransactionManager.kt:103) ~[exposed-core-0.23.1.jar:na]
	at com.invitae.cocsandbox.CocSandboxApplicationKt.main(CocSandboxApplication.kt:29) ~[main/:na]
Caused by: org.postgresql.util.PSQLException: ERROR: type "parcelstatusenum" already exists
	at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2578) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2313) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:331) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:448) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:369) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:159) ~[postgresql-42.2.11.jar:42.2.11]
	at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:125) ~[postgresql-42.2.11.jar:42.2.11]
	at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~[HikariCP-3.4.2.jar:na]
	at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-3.4.2.jar:na]
	at org.jetbrains.exposed.sql.statements.jdbc.JdbcPreparedStatementImpl.executeUpdate(JdbcPreparedStatementImpl.kt:23) ~[exposed-jdbc-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.Transaction$exec$2.executeInternal(Transaction.kt:93) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.statements.Statement.executeIn$exposed_core(Statement.kt:61) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:126) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:112) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:88) ~[exposed-core-0.23.1.jar:na]
	at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:79) ~[exposed-core-0.23.1.jar:na]
	at com.invitae.cocsandbox.CocSandboxApplicationKt$main$1.invoke(CocSandboxApplication.kt:31) ~[main/:na]
	at com.invitae.cocsandbox.CocSandboxApplicationKt$main$1.invoke(CocSandboxApplication.kt) ~[main/:na]
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction$1.invoke(ThreadLocalTransactionManager.kt:156) ~[exposed-core-0.23.1.jar:na]
	... 9 common frames omitted

outside tx
ParcelStatusEnum already exists, skipping...

rvinzent avatar Apr 02 '20 16:04 rvinzent

Can we get some attention on this?

dri94 avatar Sep 17 '20 20:09 dri94

The problem here is that exception raised on transaction commit which happens after your try-catch block. You could provide your own transaction function like:

fun transactionCommitAndCatch(statement: Transaction.()->T) = transaction {
    try {
        statement()
        commit()
    } catch (e: SQLException) {
        println(e.message)
    }
}

Tapac avatar Sep 18 '20 13:09 Tapac

This seems sort of counter-intuitive, is there a specific reason the error can't be bubbled up beyond the transaction? It's particularly cumbersome when you want to return a value from a transaction. Originally, I had something like

return newSuspendedTransaction {
         Users.insert {
                 it[username] = model.username
          } get Users.userId
}

but trying to add the try/catch inside doesn't work, because I can't return within the transaction or return the transaction result. So instead you have to use an annoying work around

var a = -1
newSuspendedTransaction {
    try {
        a = Users.insert {
            it[username] = model.username
        } get Users.userId
    } catch (e: Exception){

    }
}

return a

EDIT: This solution above doesn't even seem to work for SQLIntegrityConstraintViolationException, is there something I'm missing?

jpbulman avatar Oct 09 '20 20:10 jpbulman

I'm not sure about the exact problem you have with the provided code but the exceptions handling in suspended functions is not the same as the basic kotlin code. Please check that you doing it right by reading for example that article

Tapac avatar Oct 10 '20 21:10 Tapac

Hi guys, I hit the same issue recently. I have looked into the code and it looks like the logging is done here https://github.com/JetBrains/Exposed/blob/ca650ee196921794c7b1ff31dfe6cc2382c9dfbb/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/transactions/ThreadLocalTransactionManager.kt#L193

I assume the purpose of it is to inform about consecutive failed transaction runs. The drawback of this approach is it uses a global Exposed logger so it is not possible to switch off those log lines if we don't want to see them without switching the Exposed global log level to ERROR.

In my use case, I handle SQL duplicate exceptions (as you can see the exception is eventually thrown further in line 196) and catch that specific exception and log some informative messages. Having those intermediate messages logged clutters the logs and misleads developers to think that there is something wrong.

My current options are to switch Exposed log level to ERROR and lose all other WARN messages I might be interested in. Alternatively, I can try to meddle with the logger configuration and introduce a filter that feels dirty and can affect performance.

Preferably I would like you to reconsider logging those consecutive transaction failures and maybe concatenate them in the final exception?

adamsz-lume avatar Dec 10 '21 16:12 adamsz-lume

Not being able to effectively catch the exception from a transaction makes logs painful to read. Please please look into updating this to something more useful

minxylynx avatar Jan 27 '22 08:01 minxylynx

If I don't wrap every single transaction in try catch this library will just log everything that I passed in including the PII contained in the data in my logs. Library is used widely in our large codebase and it seems a lot of repetitive work for everyone to try catch to avoid PII, seems like a lot of work no ?

Could you provide an option instead ?

nikunjy avatar Aug 09 '22 01:08 nikunjy

Same issue. Any resolutions?

starcheek avatar Feb 17 '23 14:02 starcheek

I've noticed that Exposed's logging behavior can sometimes lead to overly verbose logs, making it difficult to find the important information. Currently, even after catching exceptions, there's still some logging output, which can make logs unnecessarily large and harder to navigate.

I believe it would be beneficial to have more fine-grained control over logging in Exposed, perhaps similar to the show-sql configuration. This would allow developers to selectively enable or disable specific log messages based on their needs

This is just a suggestion from a novice user, but I believe it could make Exposed even better and more user-friendly. Thank you for considering this suggestion.

UnforgetMemory avatar May 17 '24 09:05 UnforgetMemory