Skip to content

Commit 97c22fc

Browse files
committed
Minor improvement in ExceptionHandlerExceptionResolver
Moved a null check inside a protected method to give protected method a chance to override what happens in that case. Issues: SPR-9193
1 parent a17a889 commit 97c22fc

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
* {@link #setCustomArgumentResolvers} and {@link #setCustomReturnValueHandlers}.
5757
* Or alternatively to re-configure all argument and return value types use
5858
* {@link #setArgumentResolvers} and {@link #setReturnValueHandlers(List)}.
59-
*
59+
*
6060
* @author Rossen Stoyanchev
6161
* @since 3.1
6262
*/
@@ -73,17 +73,17 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce
7373
new ConcurrentHashMap<Class<?>, ExceptionHandlerMethodResolver>();
7474

7575
private HandlerMethodArgumentResolverComposite argumentResolvers;
76-
76+
7777
private HandlerMethodReturnValueHandlerComposite returnValueHandlers;
7878

7979
/**
8080
* Default constructor.
8181
*/
8282
public ExceptionHandlerExceptionResolver() {
83-
83+
8484
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
8585
stringHttpMessageConverter.setWriteAcceptCharset(false); // See SPR-7316
86-
86+
8787
this.messageConverters = new ArrayList<HttpMessageConverter<?>>();
8888
this.messageConverters.add(new ByteArrayHttpMessageConverter());
8989
this.messageConverters.add(stringHttpMessageConverter);
@@ -93,13 +93,13 @@ public ExceptionHandlerExceptionResolver() {
9393

9494
/**
9595
* Provide resolvers for custom argument types. Custom resolvers are ordered
96-
* after built-in ones. To override the built-in support for argument
96+
* after built-in ones. To override the built-in support for argument
9797
* resolution use {@link #setArgumentResolvers} instead.
9898
*/
9999
public void setCustomArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
100100
this.customArgumentResolvers= argumentResolvers;
101101
}
102-
102+
103103
/**
104104
* Return the custom argument resolvers, or {@code null}.
105105
*/
@@ -120,9 +120,9 @@ public void setArgumentResolvers(List<HandlerMethodArgumentResolver> argumentRes
120120
this.argumentResolvers.addResolvers(argumentResolvers);
121121
}
122122
}
123-
123+
124124
/**
125-
* Return the configured argument resolvers, or possibly {@code null} if
125+
* Return the configured argument resolvers, or possibly {@code null} if
126126
* not initialized yet via {@link #afterPropertiesSet()}.
127127
*/
128128
public HandlerMethodArgumentResolverComposite getArgumentResolvers() {
@@ -146,7 +146,7 @@ public List<HandlerMethodReturnValueHandler> getCustomReturnValueHandlers() {
146146
}
147147

148148
/**
149-
* Configure the complete list of supported return value types thus
149+
* Configure the complete list of supported return value types thus
150150
* overriding handlers that would otherwise be configured by default.
151151
*/
152152
public void setReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
@@ -158,23 +158,23 @@ public void setReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnV
158158
this.returnValueHandlers.addHandlers(returnValueHandlers);
159159
}
160160
}
161-
161+
162162
/**
163-
* Return the configured handlers, or possibly {@code null} if not
163+
* Return the configured handlers, or possibly {@code null} if not
164164
* initialized yet via {@link #afterPropertiesSet()}.
165165
*/
166166
public HandlerMethodReturnValueHandlerComposite getReturnValueHandlers() {
167167
return this.returnValueHandlers;
168168
}
169-
169+
170170
/**
171171
* Set the message body converters to use.
172172
* <p>These converters are used to convert from and to HTTP requests and responses.
173173
*/
174174
public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
175175
this.messageConverters = messageConverters;
176176
}
177-
177+
178178
/**
179179
* Return the configured message body converters.
180180
*/
@@ -199,11 +199,11 @@ public void afterPropertiesSet() {
199199
*/
200200
protected List<HandlerMethodArgumentResolver> getDefaultArgumentResolvers() {
201201
List<HandlerMethodArgumentResolver> resolvers = new ArrayList<HandlerMethodArgumentResolver>();
202-
202+
203203
// Type-based argument resolution
204204
resolvers.add(new ServletRequestMethodArgumentResolver());
205205
resolvers.add(new ServletResponseMethodArgumentResolver());
206-
206+
207207
// Custom arguments
208208
if (getCustomArgumentResolvers() != null) {
209209
resolvers.addAll(getCustomArgumentResolvers());
@@ -213,12 +213,12 @@ protected List<HandlerMethodArgumentResolver> getDefaultArgumentResolvers() {
213213
}
214214

215215
/**
216-
* Return the list of return value handlers to use including built-in and
216+
* Return the list of return value handlers to use including built-in and
217217
* custom handlers provided via {@link #setReturnValueHandlers}.
218218
*/
219219
protected List<HandlerMethodReturnValueHandler> getDefaultReturnValueHandlers() {
220220
List<HandlerMethodReturnValueHandler> handlers = new ArrayList<HandlerMethodReturnValueHandler>();
221-
221+
222222
// Single-purpose return value types
223223
handlers.add(new ModelAndViewMethodReturnValueHandler());
224224
handlers.add(new ModelMethodProcessor());
@@ -240,23 +240,18 @@ protected List<HandlerMethodReturnValueHandler> getDefaultReturnValueHandlers()
240240

241241
// Catch-all
242242
handlers.add(new ModelAttributeMethodProcessor(true));
243-
243+
244244
return handlers;
245245
}
246246

247247
/**
248-
* Find an @{@link ExceptionHandler} method and invoke it to handle the
248+
* Find an @{@link ExceptionHandler} method and invoke it to handle the
249249
* raised exception.
250250
*/
251251
@Override
252-
protected ModelAndView doResolveHandlerMethodException(HttpServletRequest request,
253-
HttpServletResponse response,
254-
HandlerMethod handlerMethod,
255-
Exception exception) {
256-
if (handlerMethod == null) {
257-
return null;
258-
}
259-
252+
protected ModelAndView doResolveHandlerMethodException(HttpServletRequest request,
253+
HttpServletResponse response, HandlerMethod handlerMethod, Exception exception) {
254+
260255
ServletInvocableHandlerMethod exceptionHandlerMethod = getExceptionHandlerMethod(handlerMethod, exception);
261256
if (exceptionHandlerMethod == null) {
262257
return null;
@@ -278,7 +273,7 @@ protected ModelAndView doResolveHandlerMethodException(HttpServletRequest reques
278273
logger.error("Failed to invoke @ExceptionHandler method: " + exceptionHandlerMethod, invocationEx);
279274
return null;
280275
}
281-
276+
282277
if (mavContainer.isRequestHandled()) {
283278
return new ModelAndView();
284279
}
@@ -288,19 +283,22 @@ protected ModelAndView doResolveHandlerMethodException(HttpServletRequest reques
288283
if (!mavContainer.isViewReference()) {
289284
mav.setView((View) mavContainer.getView());
290285
}
291-
return mav;
286+
return mav;
292287
}
293288
}
294289

295290
/**
296291
* Find the @{@link ExceptionHandler} method for the given exception.
297-
* The default implementation searches @{@link ExceptionHandler} methods
292+
* The default implementation searches @{@link ExceptionHandler} methods
298293
* in the class hierarchy of the method that raised the exception.
299-
* @param handlerMethod the method where the exception was raised
294+
* @param handlerMethod the method where the exception was raised, possibly {@code null}
300295
* @param exception the raised exception
301296
* @return a method to handle the exception, or {@code null}
302297
*/
303298
protected ServletInvocableHandlerMethod getExceptionHandlerMethod(HandlerMethod handlerMethod, Exception exception) {
299+
if (handlerMethod == null) {
300+
return null;
301+
}
304302
Class<?> handlerType = handlerMethod.getBeanType();
305303
Method method = getExceptionHandlerMethodResolver(handlerType).resolveMethod(exception);
306304
return (method != null ? new ServletInvocableHandlerMethod(handlerMethod.getBean(), method) : null);

0 commit comments

Comments
 (0)