2

我在 postgresql 数据库中有一些限制(唯一的,外键......)。

我使用弹簧数据 r2dbc 存储库:ReactiveCrudRepository

我想将DataIntegrityViolationException存储库的 throw 转换为一些基于constraintNameinErrorDetails字段的自定义异常PostgresqlDataIntegrityViolationException

但是ExceptionFactory包含的类PostgresqlDataIntegrityViolationException是包私有的。所以我不能将原因异常转换为DataIntegrityViolationExceptionto PostgresqlDataIntegrityViolationException

constraintName当我抓到 时,最干净的访问方式是DataIntegrityViolationException什么?

(比解析异常消息更好的东西^^)

编辑 :

我结束了这个解决方案:

    val DataIntegrityViolationException.pgErrorDetails: ErrorDetails
        get() = when(val cause = this.cause) {
                null -> error("cause should not be null")
                else -> cause.javaClass
                        .getDeclaredField("errorDetails")
                        .apply { isAccessible = true }
                        .let { it.get(cause) as ErrorDetails }
                }
4

0 回答 0