Skip to content

Commit 960d335

Browse files
committed
Don't wrap resolver exceptions in InvocableHandlerMethod
Prior to this commit, exceptions thrown by the `HandlerMethodArgumentResolver` would be wrapped into `IllegalStateException`s. This commit makes sure that a DEBUG log is written with the relevant information and that the root cause is not wrapped into another exception, so that the appropriate `ExceptionHandler` can be used to deal with this error. Issue: SPR-14618
1 parent e8530c9 commit 960d335

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

spring-web-reactive/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -129,7 +129,11 @@ private Mono<Object[]> resolveArguments(ServerWebExchange exchange, ModelMap mod
129129
try {
130130
return resolver.resolveArgument(param, model, exchange)
131131
.defaultIfEmpty(NO_VALUE)
132-
.otherwise(ex -> Mono.error(getArgError("Error resolving ", param, ex)))
132+
.doOnError(cause -> {
133+
if(logger.isDebugEnabled()) {
134+
logger.debug(getDetailedErrorMessage("Error resolving ", param), cause);
135+
}
136+
})
133137
.log("reactor.unresolved");
134138
}
135139
catch (Exception ex) {
@@ -148,10 +152,15 @@ private Mono<Object[]> resolveArguments(ServerWebExchange exchange, ModelMap mod
148152
}
149153

150154
private IllegalStateException getArgError(String message, MethodParameter param, Throwable cause) {
151-
return new IllegalStateException(message +
152-
"argument [" + param.getParameterIndex() + "] " +
153-
"of type [" + param.getParameterType().getName() + "] " +
154-
"on method [" + getBridgedMethod().toGenericString() + "]", cause);
155+
return new IllegalStateException(getDetailedErrorMessage(message, param), cause);
156+
}
157+
158+
private String getDetailedErrorMessage(String message, MethodParameter param) {
159+
StringBuilder sb = new StringBuilder(message);
160+
sb.append("argument [" + param.getParameterIndex() + "] ");
161+
sb.append("of type [" + param.getParameterType().getName() + "] ");
162+
sb.append("on method [" + getBridgedMethod().toGenericString() + "]");
163+
return sb.toString();
155164
}
156165

157166
private Object doInvoke(Object[] args) throws Exception {

spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/InvocableHandlerMethodTests.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
import org.springframework.web.reactive.HandlerResult;
3333
import org.springframework.web.reactive.result.ResolvableMethod;
3434
import org.springframework.web.server.ServerWebExchange;
35+
import org.springframework.web.server.UnsupportedMediaTypeStatusException;
3536
import org.springframework.web.server.adapter.DefaultServerWebExchange;
3637
import org.springframework.web.server.session.MockWebSessionManager;
3738

@@ -102,25 +103,12 @@ public void noMatchingResolver() throws Exception {
102103
@Test
103104
public void resolverThrowsException() throws Exception {
104105
InvocableHandlerMethod hm = handlerMethod("singleArg");
105-
addResolver(hm, Mono.error(new IllegalStateException("boo")));
106+
addResolver(hm, Mono.error(new UnsupportedMediaTypeStatusException("boo")));
106107
Mono<HandlerResult> mono = hm.invokeForRequest(this.exchange, this.model);
107108

108109
TestSubscriber.subscribe(mono)
109-
.assertError(IllegalStateException.class)
110-
.assertErrorMessage("Error resolving argument [0] of type [java.lang.String] " +
111-
"on method [" + hm.getMethod().toGenericString() + "]");
112-
}
113-
114-
@Test
115-
public void resolverWithErrorSignal() throws Exception {
116-
InvocableHandlerMethod hm = handlerMethod("singleArg");
117-
addResolver(hm, Mono.error(new IllegalStateException("boo")));
118-
Mono<HandlerResult> mono = hm.invokeForRequest(this.exchange, this.model);
119-
120-
TestSubscriber.subscribe(mono)
121-
.assertError(IllegalStateException.class)
122-
.assertErrorMessage("Error resolving argument [0] of type [java.lang.String] " +
123-
"on method [" + hm.getMethod().toGenericString() + "]");
110+
.assertError(UnsupportedMediaTypeStatusException.class)
111+
.assertErrorMessage("Request failure [status: 415, reason: \"boo\"]");
124112
}
125113

126114
@Test

0 commit comments

Comments
 (0)