|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2020 the original author or authors. |
| 2 | + * Copyright 2002-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.web.reactive.result.method.annotation;
|
18 | 18 |
|
| 19 | +import java.util.ArrayList; |
19 | 20 | import java.util.Collections;
|
20 | 21 | import java.util.List;
|
21 | 22 | import java.util.function.Function;
|
@@ -213,21 +214,30 @@ private Mono<HandlerResult> handleException(Throwable exception, HandlerMethod h
|
213 | 214 |
|
214 | 215 | InvocableHandlerMethod invocable = this.methodResolver.getExceptionHandlerMethod(exception, handlerMethod);
|
215 | 216 | if (invocable != null) {
|
| 217 | + ArrayList<Throwable> exceptions = new ArrayList<>(); |
216 | 218 | try {
|
217 | 219 | if (logger.isDebugEnabled()) {
|
218 | 220 | logger.debug(exchange.getLogPrefix() + "Using @ExceptionHandler " + invocable);
|
219 | 221 | }
|
220 | 222 | bindingContext.getModel().asMap().clear();
|
221 |
| - Throwable cause = exception.getCause(); |
222 |
| - if (cause != null) { |
223 |
| - return invocable.invoke(exchange, bindingContext, exception, cause, handlerMethod); |
224 |
| - } |
225 |
| - else { |
226 |
| - return invocable.invoke(exchange, bindingContext, exception, handlerMethod); |
| 223 | + |
| 224 | + // Expose causes as provided arguments as well |
| 225 | + Throwable exToExpose = exception; |
| 226 | + while (exToExpose != null) { |
| 227 | + exceptions.add(exToExpose); |
| 228 | + Throwable cause = exToExpose.getCause(); |
| 229 | + exToExpose = (cause != exToExpose ? cause : null); |
227 | 230 | }
|
| 231 | + Object[] arguments = new Object[exceptions.size() + 1]; |
| 232 | + exceptions.toArray(arguments); // efficient arraycopy call in ArrayList |
| 233 | + arguments[arguments.length - 1] = handlerMethod; |
| 234 | + |
| 235 | + return invocable.invoke(exchange, bindingContext, arguments); |
228 | 236 | }
|
229 | 237 | catch (Throwable invocationEx) {
|
230 |
| - if (logger.isWarnEnabled()) { |
| 238 | + // Any other than the original exception (or a cause) is unintended here, |
| 239 | + // probably an accident (e.g. failed assertion or the like). |
| 240 | + if (!exceptions.contains(invocationEx) && logger.isWarnEnabled()) { |
231 | 241 | logger.warn(exchange.getLogPrefix() + "Failure in @ExceptionHandler " + invocable, invocationEx);
|
232 | 242 | }
|
233 | 243 | }
|
|
0 commit comments