I have some constraints in a postgresql database (unique, foreign key...).
I use spring data r2dbc repository : ReactiveCrudRepository
I would like to transform the DataIntegrityViolationException
throw by the repository to some custom exception based on the constraintName
in ErrorDetails
field of PostgresqlDataIntegrityViolationException
.
But ExceptionFactory
class containing the PostgresqlDataIntegrityViolationException
is package private. So I can't cast the cause exception of DataIntegrityViolationException
to PostgresqlDataIntegrityViolationException
.
What is the cleanest way to access to the constraintName
when I catch a DataIntegrityViolationException
?
(something better than parsing the exception message ^^)
edit :
I'm ended with this solution :
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 }
}