Skip to content

Commit

Permalink
Gateway: Propagate Error to the errorChannel
Browse files Browse the repository at this point in the history
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**
  • Loading branch information
artembilan committed Oct 22, 2020
1 parent fb35a73 commit b94c20a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ private Object doSendAndReceive(Object object, boolean shouldConvert) {
sample.stop(sendTimer());
}
}
catch (Exception ex) {
catch (Throwable ex) { // NOSONAR (catch throwable)
if (logger.isDebugEnabled()) {
logger.debug("failure occurred in gateway sendAndReceive: " + ex.getMessage());
}
Expand Down Expand Up @@ -566,6 +566,9 @@ private Object handleSendAndReceiveError(Object object, @Nullable Message<?> req
: errorFlowReply;
}
}
else if (error instanceof Error) {
throw (Error) error;
}
else {
Throwable errorToReThrow = error;
if (error instanceof MessagingException &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package org.springframework.integration.gateway;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.Mockito.mock;

import java.time.Duration;
Expand All @@ -27,7 +27,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.integration.annotation.Gateway;
Expand Down Expand Up @@ -88,13 +88,10 @@ protected boolean doSend(Message<?> message, long timeout) {
proxyFactory.afterPropertiesSet();
TestEchoService service = (TestEchoService) proxyFactory.getObject();
Future<Message<?>> f = service.returnMessage("foo");
try {
f.get(10000, TimeUnit.MILLISECONDS);
fail("Expected Exception");
}
catch (ExecutionException e) {
assertThat(e.getCause()).isEqualTo(error);
}

assertThatExceptionOfType(ExecutionException.class)
.isThrownBy(() -> f.get(10000, TimeUnit.MILLISECONDS))
.withCause(error);
}

@Test
Expand Down Expand Up @@ -134,7 +131,7 @@ public void onFailure(Throwable t) {
}

@Test
public void customFutureReturned() throws Exception {
public void customFutureReturned() {
QueueChannel requestChannel = new QueueChannel();
addThreadEnricher(requestChannel);
startResponder(requestChannel);
Expand Down

0 comments on commit b94c20a

Please sign in to comment.