Gateway now rethrows unwrapped RuntimeExceptions

This commit is contained in:
Mark Fisher
2011-08-17 12:29:30 -04:00
parent a7e4fc104b
commit e954e58e93
3 changed files with 32 additions and 15 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ package org.springframework.integration.gateway;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@@ -310,15 +311,21 @@ public class GatewayProxyFactoryBean extends AbstractEndpoint implements Trackab
private void rethrowExceptionInThrowsClauseIfPossible(Throwable originalException, Method method) throws Throwable {
List<Class<?>> exceptionTypes = Arrays.asList(method.getExceptionTypes());
Throwable t = originalException;
while (t != null) {
if (exceptionTypes.contains(t.getClass())) {
while (t instanceof MessagingException || t instanceof UndeclaredThrowableException) {
t = t.getCause();
}
if (t instanceof RuntimeException) {
throw t;
}
for (Class<?> exceptionType : exceptionTypes) {
if (exceptionType.isAssignableFrom(t.getClass())) {
throw t;
}
t = t.getCause();
}
throw originalException;
}
private MethodInvocationGateway createGatewayForMethod(Method method) {
Gateway gatewayAnnotation = method.getAnnotation(Gateway.class);
MessageChannel requestChannel = this.defaultRequestChannel;

View File

@@ -104,8 +104,8 @@ public class GatewayInvokingMessageHandlerTests {
gatewayWithError.process("echoWithRuntimeExceptionChannel");
Assert.fail();
}
catch (MessageHandlingException e) {
Assert.assertEquals("echoWithRuntimeExceptionChannel", e.getFailedMessage().getPayload());
catch (SampleRuntimeException e) {
Assert.assertEquals("echoWithRuntimeExceptionChannel", e.getMessage());
}
try {
@@ -133,7 +133,7 @@ public class GatewayInvokingMessageHandlerTests {
Assert.fail();
}
catch (Exception e) {
Assert.assertTrue(e instanceof MessageHandlingException);
Assert.assertEquals(SampleRuntimeException.class, e.getClass());
}
}
@@ -164,27 +164,38 @@ public class GatewayInvokingMessageHandlerTests {
public static class SimpleService {
public String echo(String value) {
return "echo:" + value;
}
public RuntimeException echoWithRuntimeException(String value) {
throw new RuntimeException(value);
public String echoWithRuntimeException(String value) {
throw new SampleRuntimeException(value);
}
public MessageHandlingException echoWithMessagingException(String value) {
public String echoWithMessagingException(String value) {
throw new MessageHandlingException(new GenericMessage<String>(value));
}
public RuntimeException echoWithErrorAsync(String value) {
throw new RuntimeException(value);
public String echoWithErrorAsync(String value) {
throw new SampleRuntimeException(value);
}
}
@SuppressWarnings("serial")
public static class SampleCheckedException extends Exception {
public SampleCheckedException(String message){
public SampleCheckedException(String message) {
super(message);
}
}
@SuppressWarnings("serial")
public static class SampleRuntimeException extends RuntimeException {
public SampleRuntimeException(String message) {
super(message);
}
}
}

View File

@@ -26,7 +26,6 @@ import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.Message;
import org.springframework.integration.MessageHandlingException;
import org.springframework.integration.core.MessageHandler;
import org.springframework.integration.core.SubscribableChannel;
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
@@ -84,7 +83,7 @@ public class InnerGatewayWithChainTests {
}
// if no error channels explicitly defined exception is rethrown
@Test(expected=MessageHandlingException.class)
@Test(expected=ArithmeticException.class)
public void testGatewaysNoErrorChannel(){
testGatewayWithNoErrorChannelAAA.echo(0);
}