Skip to content

Conversation

andreconrado
Copy link

@andreconrado andreconrado commented Aug 25, 2025

Pull request to fix the issue: #655

Refactor indexViolation to handle wrapped exceptions and improve clarity.

  • Unwrap UndeclaredThrowableException for proxy-related exceptions.
  • Unwrap InvocationTargetException for reflection-related exceptions.
  • Add null-safe handling for exception messages.
  • Improve readability of PostgreSQL-specific checks.
  • Improve readability of SQL Server-specific checks.
  • Ensure compatibility with standard JDBC constraint violations.

…arity.

- Unwrap `UndeclaredThrowableException` for proxy-related exceptions.
- Unwrap `InvocationTargetException` for reflection-related exceptions.
- Add null-safe handling for exception messages.
- Improve readability of PostgreSQL-specific checks.
- Improve readability of SQL Server-specific checks.
- Ensure compatibility with standard JDBC constraint violations.
@badgerwithagun
Copy link
Member

Hey @andreconrado . This looks great. Could you add a cross-dialect test for this? Adding it to AbstractPersistorTest should mean it's tested for all dialects.

@badgerwithagun
Copy link
Member

Is this actually to fix #655 ?

@andreconrado andreconrado changed the title Pull request to fix the issue: #866 Pull request to fix the issue: #655 Aug 26, 2025
@andreconrado
Copy link
Author

Yes, sorry, I had it wrong in the title and description, I copied the wrong link 😁
I’ve changed it now.

@andreconrado
Copy link
Author

Hi @badgerwithagun,

I only added some validations before checking the source of the Exception, so the existing tests should confirm that my changes work - or at least don’t affect what was already there.

In this case, do I need to change the tests?
That would mean more work/time, since I’d have to analyze how to modify/create the test.

@badgerwithagun
Copy link
Member

@andreconrado

It's pretty straightforward; I'm happy to help.

Obviously for the existing scenarios we have this test.

You've added support for wrapped exceptions, so all we need to do is create a version of that test which causes the exception to be wrapped. First thing I would try is something like this.

In this new test, copy the existing one but change:

txManager().inTransactionThrows(tx -> persistor().save(tx, entry1));

To

txManager().inTransactionThrows(tx -> {
  var wrappedTxn =  Mockito.spy(tx);
  Mockito.when(wrappedTxn.prepareBatchStatement(any()))
    .thenAnswer(args -> {
      var wrappedStmt = Mockito.spy(args.callRealMethod());
      Mockito.doAnswer(args -> {
        try {
          args.callRealMethod();
          return null;
        } catch (Exception e) {
          throw new UndeclaredThrowableException(e);
        }
      }).when(wrappedStmt.executeUpdate());
      return wrappedStmt;
    });
  persistor().save(wrappedTxn, entry1);
});

This should result in any exceptions in the save method being wrapped in UndeclaredThrowableException. Not tested the code. Probably doesn't compile. But should speed things along.

@andreconrado
Copy link
Author

andreconrado commented Aug 26, 2025

Hi @badgerwithagun, thanks for the tips!

I think I’ll still need your help. The line var wrappedTxn = Mockito.spy(tx); gives an error:

Caused by: org.mockito.exceptions.base.MockitoException: 
Cannot mock/spy class com.gruelbox.transactionoutbox.spi.SimpleTransaction
Mockito cannot mock/spy the following:

The SimpleTransaction class is final, so I can’t mock it. Is there an example I can use as a reference to mock this?

@andreconrado
Copy link
Author

Hi again @badgerwithagun,

To be able to run the test, we need to change the dependency from:

<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-all</artifactId>
  <version>1.10.19</version>
  <scope>test</scope>
</dependency>

to:

<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-core</artifactId>
  <version>5.19.0</version>
  <scope>test</scope>
</dependency>

Are you interested in making this change? Is it worth it just for this test?

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants