Support HandlerMethod parameter in @MessageExceptionHandler

Issue: SPR-13196
This commit is contained in:
Sebastien Deleuze
2015-07-07 15:24:47 +02:00
parent f79a5c12d5
commit 97936140cc
2 changed files with 27 additions and 1 deletions

View File

@@ -493,7 +493,7 @@ public abstract class AbstractMethodMessageHandler<T>
logger.debug("Invoking " + invocable.getShortLogMessage());
}
try {
Object returnValue = invocable.invoke(message, ex);
Object returnValue = invocable.invoke(message, ex, handlerMethod);
MethodParameter returnType = invocable.getReturnType();
if (void.class == returnType.getParameterType()) {
return;

View File

@@ -39,6 +39,7 @@ import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.SubscribableChannel;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.handler.HandlerMethod;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Headers;
@@ -207,6 +208,20 @@ public class SimpAnnotationMethodMessageHandlerTests {
assertEquals("handleValidationException", this.testController.method);
}
@Test
public void exceptionWithHandlerMethodArg() {
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create();
headers.setSessionId("session1");
headers.setSessionAttributes(new ConcurrentHashMap<>());
headers.setDestination("/pre/illegalState");
Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
this.messageHandler.handleMessage(message);
assertEquals("handleExceptionWithHandlerMethodArg", this.testController.method);
HandlerMethod handlerMethod = (HandlerMethod) this.testController.arguments.get("handlerMethod");
assertNotNull(handlerMethod);
assertEquals("illegalState", handlerMethod.getMethod().getName());
}
@Test
public void simpScope() {
Map<String, Object> map = new ConcurrentHashMap<>();
@@ -405,11 +420,22 @@ public class SimpAnnotationMethodMessageHandlerTests {
this.arguments.put("message", payload);
}
@MessageMapping("/illegalState")
public void illegalState() {
throw new IllegalStateException();
}
@MessageExceptionHandler(MethodArgumentNotValidException.class)
public void handleValidationException() {
this.method = "handleValidationException";
}
@MessageExceptionHandler(IllegalStateException.class)
public void handleExceptionWithHandlerMethodArg(HandlerMethod handlerMethod) {
this.method = "handleExceptionWithHandlerMethodArg";
this.arguments.put("handlerMethod", handlerMethod);
}
@MessageMapping("/scope")
public void scope() {
SimpAttributes simpAttributes = SimpAttributesContextHolder.currentAttributes();