Skip to content

Commit b94c20a

Browse files
authored
Gateway: Propagate Error to the errorChannel
See SO for more info: https://stackoverflow.com/questions/64456946/handle-exceptions-errors-other-than-messagingexception-ie-other-error-excepti In the versions before `5.2.x` the `Error` was wrapped to the `MessagingException` in the `AbstractRequestHandlerAdvice` and this one was handled properly on the gateway level with its `errorChannel` configured * Fix `MessagingGatewaySupport` to catch all the `Throwable` and try to send them as `ErrorMessage` to the `errorChannel`. Otherwise unwrap returned `MessagingException` and re-throw an `Error` as is to keep the current behavior for non-handled exceptions * Do not wrap `Error` into a `MessagingException` and re-throw as is if there is no `errorChannel` **Cherry-pick to 5.3.x & 5.2.x**
1 parent fb35a73 commit b94c20a

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

spring-integration-core/src/main/java/org/springframework/integration/gateway/MessagingGatewaySupport.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ private Object doSendAndReceive(Object object, boolean shouldConvert) {
528528
sample.stop(sendTimer());
529529
}
530530
}
531-
catch (Exception ex) {
531+
catch (Throwable ex) { // NOSONAR (catch throwable)
532532
if (logger.isDebugEnabled()) {
533533
logger.debug("failure occurred in gateway sendAndReceive: " + ex.getMessage());
534534
}
@@ -566,6 +566,9 @@ private Object handleSendAndReceiveError(Object object, @Nullable Message<?> req
566566
: errorFlowReply;
567567
}
568568
}
569+
else if (error instanceof Error) {
570+
throw (Error) error;
571+
}
569572
else {
570573
Throwable errorToReThrow = error;
571574
if (error instanceof MessagingException &&

spring-integration-core/src/test/java/org/springframework/integration/gateway/AsyncGatewayTests.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.springframework.integration.gateway;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.assertj.core.api.Assertions.fail;
20+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2121
import static org.mockito.Mockito.mock;
2222

2323
import java.time.Duration;
@@ -27,7 +27,7 @@
2727
import java.util.concurrent.TimeUnit;
2828
import java.util.concurrent.atomic.AtomicReference;
2929

30-
import org.junit.Test;
30+
import org.junit.jupiter.api.Test;
3131

3232
import org.springframework.beans.factory.BeanFactory;
3333
import org.springframework.integration.annotation.Gateway;
@@ -88,13 +88,10 @@ protected boolean doSend(Message<?> message, long timeout) {
8888
proxyFactory.afterPropertiesSet();
8989
TestEchoService service = (TestEchoService) proxyFactory.getObject();
9090
Future<Message<?>> f = service.returnMessage("foo");
91-
try {
92-
f.get(10000, TimeUnit.MILLISECONDS);
93-
fail("Expected Exception");
94-
}
95-
catch (ExecutionException e) {
96-
assertThat(e.getCause()).isEqualTo(error);
97-
}
91+
92+
assertThatExceptionOfType(ExecutionException.class)
93+
.isThrownBy(() -> f.get(10000, TimeUnit.MILLISECONDS))
94+
.withCause(error);
9895
}
9996

10097
@Test
@@ -134,7 +131,7 @@ public void onFailure(Throwable t) {
134131
}
135132

136133
@Test
137-
public void customFutureReturned() throws Exception {
134+
public void customFutureReturned() {
138135
QueueChannel requestChannel = new QueueChannel();
139136
addThreadEnricher(requestChannel);
140137
startResponder(requestChannel);

0 commit comments

Comments
 (0)