Skip to content

Allow access to io.r2dbc.postgresql.ExceptionFactory exceptions from client code #644

@loehnertz

Description

@loehnertz

Feature Request

Is your feature request related to a problem? Please describe

When using this driver via Spring, when exceptions that this driver throws occur, Spring wraps them into wrapper exceptions such that different drivers can be used (e.g., JPA, Hibernate, R2DBC, etc.).

My example is the org.springframework.dao.DataIntegrityViolationException (the name implies when it occurs), which wraps around a Throwable cause that the driver threw. When using this R2DBC driver, that cause is io.r2dbc.postgresql.ExceptionFactory.PostgresqlDataIntegrityViolationException.

My use case was to access which constraint was violated in an UPDATE query to rethrow the exception with a specific human-readable error message (instead of violates unique constraint \"%s\").
Sadly, since all the exceptions produced by the ExceptionFactory are non-public, I cannot import them into my client code to perform a cast of the Throwable cause of Spring's exception to then access the ErrorDetails of the driver's exception which would contain the constraint that was violated which I am after, such as:

catch (org.springframework.dao.DataIntegrityViolationException exception) {
  Throwable cause = exception.getCause();
  // The below line does not compile as the exception cannot be imported or otherwise accessed.
  if (cause instanceof io.r2dbc.postgresql.ExceptionFactory.PostgresqlDataIntegrityViolationException) {
   if (cause.getErrorDetails().getConstraintName().map(cn -> cn.equals("only_one_version_enabled_idx")).orElse(false)) {
      throw new IllegalArgumentException("Only one version can be active at a time", exception);
   }
  }
  throw exception;
}

Unfortunately, this code does not compile.

Describe the solution you'd like

If the exceptions produced by io.r2dbc.postgresql.ExceptionFactory were public, the above example code would work.

For instance, Hibernate or JPA do support this "correctly" via e.g., HibernateExceptionTranslator that will (to stay with my example) bubble up a org.hibernate.exception.ConstraintViolationException as the Throwable cause of Spring's wrapper exception which is public, thus making this code compile (in current Java versions):

catch (org.springframework.dao.DataIntegrityViolationException exception) {
  Throwable cause = exception.getCause();
  if (cause instanceof org.hibernate.exception.ConstraintViolationException) {
   if ("only_one_version_enabled_idx".equals(cause.getConstraintName())) {
      throw new IllegalArgumentException("Only one version can be active at a time", exception);
   }
  }
  throw exception;
}

Describe alternatives you've considered

N/A

Teachability, Documentation, Adoption, Migration Strategy

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions