<@U01SP2GJYAU> It seems like DryRunResult's sql wh...
# komapper
d
@Toshihiro Nakamura It seems like DryRunResult's sql when an error was thrown on a query with a flatMap returns the first query even though the error seems to be in the query inside the flatMap -- that makes sense since the dryRun can't have any idea of a runtime failure, but it makes it impossible to get a normal log with the sql and params being run when getting such a runtime failure... unless I'm missing something? (Regular runtime failures only give a small part of the sql that had bad grammar... which isn't too helpful in tracking down the problem)
This is what I tried (not the best implementation -- but I tried something quick for now):
Copy code
suspend fun <T> R2dbcDatabase.runSafeQuery(query: Query<T>): T = try {
        runQuery(query)
    } catch (e: Exception) {
        val dryRunResult = query.dryRun(db.config)
        if (dryRunResult.throwable != null) {
            val querySql = dryRunResult.sql + " [" + dryRunResult.args.joinToString(",") { it.any.toString() } + "]"
            logger.error(dryRunResult.throwable) { "Failed to run query: $querySql" }
        } else {
            logger.error(e) { "Failed to run query: ${dryRunResult.sqlWithArgs}" }
            throw e
        }
        throw e
    }
My failure fell into the else block
t
Komapper provides a feature to log SQL statements. https://www.komapper.org/docs/reference/logging/
Regular runtime failures only give a small part of the sql that had bad grammar... which isn’t too helpful in tracking down the problem
When an exception occurs in the R2DBC driver, Komapper returns that exception to the caller. It might be helpful to ensure that the R2DBC driver is configured to include sufficient information in the exception.
d
@Toshihiro Nakamura I've used that feature in the past for testing and staging environments, but for production, there's just too much logs being outputted... I'm really only interested in printing out sql when there's a failure... the R2Dbc exception doesn't seem to have enough information in it (unless I'm missing something?).
t
Komapper converts exceptions thrown by the R2DBC driver. https://github.com/komapper/komapper/blob/v5.2.0/komapper-r2dbc/src/main/kotlin/org/komapper/r2dbc/R2dbcExecutor.kt#L157 We can consider modifying this code to throw an exception that includes an
org.komapper.core.Statement
object. If this change were implemented, would it meet your expectations? Please note that the type of the thrown exception will change, which could affect any code that uses exception types for conditional branching.
d
That could potentially be very nice! Is there a way to get the Sql and args out of Statement? So far, I've only dealt with dryRuns on Queries... also I don't really rely on those specific exceptions anyways... I'm just wondering about other users... maybe a new exception could be derived from the R2dbc one?