Skip to content

Commit 2fffcce

Browse files
committed
[#2518] Remove CompletionException as cause of exception
1 parent 2739325 commit 2fffcce

File tree

2 files changed

+49
-64
lines changed

2 files changed

+49
-64
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/session/impl/ReactiveSessionImpl.java

Lines changed: 42 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@
151151
import static org.hibernate.reactive.util.impl.CompletionStages.nullFuture;
152152
import static org.hibernate.reactive.util.impl.CompletionStages.rethrow;
153153
import static org.hibernate.reactive.util.impl.CompletionStages.returnNullorRethrow;
154-
import static org.hibernate.reactive.util.impl.CompletionStages.returnOrRethrow;
155154
import static org.hibernate.reactive.util.impl.CompletionStages.supplyStage;
156155
import static org.hibernate.reactive.util.impl.CompletionStages.voidFuture;
157156

@@ -171,10 +170,7 @@ public class ReactiveSessionImpl extends SessionImpl implements ReactiveSession,
171170
//Lazily initialized
172171
private transient ExceptionConverter exceptionConverter;
173172

174-
public ReactiveSessionImpl(
175-
SessionFactoryImpl delegate,
176-
SessionCreationOptions options,
177-
ReactiveConnection connection) {
173+
public ReactiveSessionImpl(SessionFactoryImpl delegate, SessionCreationOptions options, ReactiveConnection connection) {
178174
super( delegate, options );
179175
InternalStateAssertions.assertUseOnEventLoop();
180176
this.associatedWorkThread = Thread.currentThread();
@@ -977,20 +973,13 @@ private CompletionStage<Void> fireRemove(DeleteEvent event) {
977973

978974
return getFactory().getEventListenerGroups().eventListenerGroup_DELETE
979975
.fireEventOnEachListener( event, (ReactiveDeleteEventListener l) -> l::reactiveOnDelete )
980-
.handle( (v, e) -> {
976+
.handle( CompletionStages::handle )
977+
.thenCompose( handler -> {
981978
delayedAfterCompletion();
982-
983-
if ( e instanceof ObjectDeletedException ) {
984-
throw getExceptionConverter().convert( new IllegalArgumentException( e ) );
985-
}
986-
else if ( e instanceof MappingException ) {
987-
throw getExceptionConverter().convert( new IllegalArgumentException( e.getMessage(), e ) );
988-
}
989-
else if ( e instanceof RuntimeException ) {
990-
//including HibernateException
991-
throw getExceptionConverter().convert( (RuntimeException) e );
992-
}
993-
return returnNullorRethrow( e );
979+
final Throwable e = handler.getThrowable();
980+
return e != null
981+
? failedFuture( convertException( e ) )
982+
: voidFuture();
994983
} );
995984
}
996985

@@ -999,20 +988,13 @@ private CompletionStage<Void> fireRemove(DeleteEvent event, DeleteContext transi
999988

1000989
return getFactory().getEventListenerGroups().eventListenerGroup_DELETE
1001990
.fireEventOnEachListener( event, transientEntities, (ReactiveDeleteEventListener l) -> l::reactiveOnDelete )
1002-
.handle( (v, e) -> {
991+
.handle( CompletionStages::handle )
992+
.thenCompose( handler -> {
1003993
delayedAfterCompletion();
1004-
1005-
if ( e instanceof ObjectDeletedException ) {
1006-
throw getExceptionConverter().convert( new IllegalArgumentException( e ) );
1007-
}
1008-
else if ( e instanceof MappingException ) {
1009-
throw getExceptionConverter().convert( new IllegalArgumentException( e.getMessage(), e ) );
1010-
}
1011-
else if ( e instanceof RuntimeException ) {
1012-
//including HibernateException
1013-
throw getExceptionConverter().convert( (RuntimeException) e );
1014-
}
1015-
return returnNullorRethrow( e );
994+
final Throwable e = handler.getThrowable();
995+
return e != null
996+
? failedFuture( convertException( e ) )
997+
: voidFuture();
1016998
} );
1017999
}
10181000

@@ -1036,42 +1018,45 @@ private <T> CompletionStage<T> fireMerge(MergeEvent event) {
10361018

10371019
return getFactory().getEventListenerGroups().eventListenerGroup_MERGE
10381020
.fireEventOnEachListener( event, (ReactiveMergeEventListener l) -> l::reactiveOnMerge )
1039-
.handle( (v, e) -> {
1021+
.handle( CompletionStages::handle )
1022+
.thenCompose( handler -> {
10401023
checkNoUnresolvedActionsAfterOperation();
1041-
1042-
if ( e instanceof ObjectDeletedException ) {
1043-
throw getExceptionConverter().convert( new IllegalArgumentException( e ) );
1044-
}
1045-
else if ( e instanceof MappingException ) {
1046-
throw getExceptionConverter().convert( new IllegalArgumentException( e.getMessage(), e ) );
1047-
}
1048-
else if ( e instanceof RuntimeException ) {
1049-
//including HibernateException
1050-
throw getExceptionConverter().convert( (RuntimeException) e );
1051-
}
1052-
return returnOrRethrow( e, (T) event.getResult() );
1024+
final Throwable e = handler.getThrowable();
1025+
return e != null
1026+
? failedFuture( convertException( e ) )
1027+
: completedFuture( (T) event.getResult() );
10531028
} );
10541029
}
10551030

1031+
private Throwable convertException(Throwable e) {
1032+
if ( e instanceof CompletionException) {
1033+
return convertException( e.getCause() );
1034+
}
1035+
if ( e instanceof ObjectDeletedException ) {
1036+
return getExceptionConverter().convert( new IllegalArgumentException( e ) );
1037+
}
1038+
if ( e instanceof MappingException ) {
1039+
return getExceptionConverter().convert( new IllegalArgumentException( e.getMessage(), e ) );
1040+
}
1041+
if ( e instanceof RuntimeException ) {
1042+
//including HibernateException
1043+
return getExceptionConverter().convert( (RuntimeException) e );
1044+
}
1045+
return e;
1046+
}
1047+
10561048
private CompletionStage<Void> fireMerge(MergeContext copiedAlready, MergeEvent event) {
10571049
pulseTransactionCoordinator();
10581050

10591051
return getFactory().getEventListenerGroups().eventListenerGroup_MERGE
10601052
.fireEventOnEachListener( event, copiedAlready,(ReactiveMergeEventListener l) -> l::reactiveOnMerge )
1061-
.handle( (v, e) -> {
1053+
.handle( CompletionStages::handle )
1054+
.thenCompose( handler -> {
10621055
delayedAfterCompletion();
1063-
1064-
if ( e instanceof ObjectDeletedException ) {
1065-
throw getExceptionConverter().convert( new IllegalArgumentException( e ) );
1066-
}
1067-
else if ( e instanceof MappingException ) {
1068-
throw getExceptionConverter().convert( new IllegalArgumentException( e.getMessage(), e ) );
1069-
}
1070-
else if ( e instanceof RuntimeException ) {
1071-
//including HibernateException
1072-
throw getExceptionConverter().convert( (RuntimeException) e );
1073-
}
1074-
return returnNullorRethrow( e );
1056+
final Throwable e = handler.getThrowable();
1057+
return e != null
1058+
? failedFuture( convertException( e ) )
1059+
: voidFuture();
10751060
} );
10761061
}
10771062

hibernate-reactive-core/src/test/java/org/hibernate/reactive/ReactiveSessionTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.concurrent.CompletionException;
1111
import java.util.concurrent.CompletionStage;
1212

13+
import org.hibernate.HibernateException;
1314
import org.hibernate.LockMode;
1415
import org.hibernate.reactive.common.AffectedEntities;
1516
import org.hibernate.reactive.stage.Stage;
@@ -29,6 +30,7 @@
2930

3031
import static java.util.concurrent.TimeUnit.MINUTES;
3132
import static org.assertj.core.api.Assertions.assertThat;
33+
import static org.hibernate.reactive.testing.ReactiveAssertions.assertThrown;
3234
import static org.hibernate.reactive.util.impl.CompletionStages.voidFuture;
3335

3436
@Timeout(value = 10, timeUnit = MINUTES)
@@ -619,14 +621,12 @@ public void reactiveRemoveTransientEntity(VertxTestContext context) {
619621
.thenCompose( v -> selectNameFromId( 5 ) )
620622
.thenAccept( result -> assertThat( result ).isNotNull() )
621623
.thenCompose( v -> openSession() )
622-
.thenCompose( session -> session.remove( new GuineaPig( 5, "Aloi" ) )
623-
.thenCompose( v -> session.flush() )
624-
.thenCompose( v -> session.close() )
624+
.thenCompose( session -> assertThrown( HibernateException.class, session.remove( new GuineaPig( 5, "Aloi" ) ) )
625+
)
626+
.thenAccept( t -> assertThat( t )
627+
.hasCauseInstanceOf( IllegalArgumentException.class )
628+
.hasMessageContaining( "unmanaged instance" )
625629
)
626-
.handle( (r, e) -> {
627-
assertNotNull( e );
628-
return r;
629-
} )
630630
);
631631
}
632632

0 commit comments

Comments
 (0)