151151import static org .hibernate .reactive .util .impl .CompletionStages .nullFuture ;
152152import static org .hibernate .reactive .util .impl .CompletionStages .rethrow ;
153153import static org .hibernate .reactive .util .impl .CompletionStages .returnNullorRethrow ;
154- import static org .hibernate .reactive .util .impl .CompletionStages .returnOrRethrow ;
155154import static org .hibernate .reactive .util .impl .CompletionStages .supplyStage ;
156155import 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
0 commit comments