Skip to content

Commit 9793614

Browse files
committed
Support HandlerMethod parameter in @MessageExceptionHandler
Issue: SPR-13196
1 parent f79a5c1 commit 9793614

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ protected void processHandlerMethodException(HandlerMethod handlerMethod, Except
493493
logger.debug("Invoking " + invocable.getShortLogMessage());
494494
}
495495
try {
496-
Object returnValue = invocable.invoke(message, ex);
496+
Object returnValue = invocable.invoke(message, ex, handlerMethod);
497497
MethodParameter returnType = invocable.getReturnType();
498498
if (void.class == returnType.getParameterType()) {
499499
return;

spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandlerTests.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.messaging.MessageHeaders;
4040
import org.springframework.messaging.SubscribableChannel;
4141
import org.springframework.messaging.converter.MessageConverter;
42+
import org.springframework.messaging.handler.HandlerMethod;
4243
import org.springframework.messaging.handler.annotation.DestinationVariable;
4344
import org.springframework.messaging.handler.annotation.Header;
4445
import org.springframework.messaging.handler.annotation.Headers;
@@ -207,6 +208,20 @@ public void validationError() {
207208
assertEquals("handleValidationException", this.testController.method);
208209
}
209210

211+
@Test
212+
public void exceptionWithHandlerMethodArg() {
213+
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create();
214+
headers.setSessionId("session1");
215+
headers.setSessionAttributes(new ConcurrentHashMap<>());
216+
headers.setDestination("/pre/illegalState");
217+
Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
218+
this.messageHandler.handleMessage(message);
219+
assertEquals("handleExceptionWithHandlerMethodArg", this.testController.method);
220+
HandlerMethod handlerMethod = (HandlerMethod) this.testController.arguments.get("handlerMethod");
221+
assertNotNull(handlerMethod);
222+
assertEquals("illegalState", handlerMethod.getMethod().getName());
223+
}
224+
210225
@Test
211226
public void simpScope() {
212227
Map<String, Object> map = new ConcurrentHashMap<>();
@@ -405,11 +420,22 @@ public void payloadValidation(@Validated @Payload String payload) {
405420
this.arguments.put("message", payload);
406421
}
407422

423+
@MessageMapping("/illegalState")
424+
public void illegalState() {
425+
throw new IllegalStateException();
426+
}
427+
408428
@MessageExceptionHandler(MethodArgumentNotValidException.class)
409429
public void handleValidationException() {
410430
this.method = "handleValidationException";
411431
}
412432

433+
@MessageExceptionHandler(IllegalStateException.class)
434+
public void handleExceptionWithHandlerMethodArg(HandlerMethod handlerMethod) {
435+
this.method = "handleExceptionWithHandlerMethodArg";
436+
this.arguments.put("handlerMethod", handlerMethod);
437+
}
438+
413439
@MessageMapping("/scope")
414440
public void scope() {
415441
SimpAttributes simpAttributes = SimpAttributesContextHolder.currentAttributes();

0 commit comments

Comments
 (0)