From 66a3a82fb31b610e63bb27e7ddef02c2b63d8c50 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sun, 1 Apr 2018 00:21:15 +0200 Subject: [PATCH] Avoid reference to HandlerMethod class in ServerErrorException This breaks the package dependency cycle between web.server/web.method and makes ServerErrorException more generally applicable. Includes deprecation of the plain reason constructor variant, in favor of providing a Method or MethodParameter context (which MatrixVariableMethodArgumentResolver does now). --- .../web/server/ServerErrorException.java | 48 ++++++++++--------- .../method/SyncInvocableHandlerMethod.java | 2 +- .../MatrixVariableMethodArgumentResolver.java | 3 +- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/server/ServerErrorException.java b/spring-web/src/main/java/org/springframework/web/server/ServerErrorException.java index 523b44d7513..5f18a9f0213 100644 --- a/spring-web/src/main/java/org/springframework/web/server/ServerErrorException.java +++ b/spring-web/src/main/java/org/springframework/web/server/ServerErrorException.java @@ -16,10 +16,11 @@ package org.springframework.web.server; +import java.lang.reflect.Method; + import org.springframework.core.MethodParameter; import org.springframework.http.HttpStatus; import org.springframework.lang.Nullable; -import org.springframework.web.method.HandlerMethod; /** * Exception for an {@link HttpStatus#INTERNAL_SERVER_ERROR} that exposes extra @@ -33,49 +34,41 @@ import org.springframework.web.method.HandlerMethod; public class ServerErrorException extends ResponseStatusException { @Nullable - private final HandlerMethod handlerMethod; + private final Method handlerMethod; @Nullable private final MethodParameter parameter; /** - * Constructor with an explanation only. + * Constructor for a 500 error with a reason and an optional cause. + * @since 5.0.5 */ - public ServerErrorException(String reason) { - super(HttpStatus.INTERNAL_SERVER_ERROR, reason, null); + public ServerErrorException(String reason, @Nullable Throwable cause) { + super(HttpStatus.INTERNAL_SERVER_ERROR, reason, cause); this.handlerMethod = null; this.parameter = null; } /** - * Constructor with a reason and root cause. + * Constructor for a 500 error with a handler {@link Method} and an optional cause. * @since 5.0.5 */ - public ServerErrorException(String reason, Throwable cause) { + public ServerErrorException(String reason, Method handlerMethod, @Nullable Throwable cause) { super(HttpStatus.INTERNAL_SERVER_ERROR, reason, cause); - this.handlerMethod = null; + this.handlerMethod = handlerMethod; this.parameter = null; } /** - * Constructor for a 500 error with a {@link MethodParameter}. + * Constructor for a 500 error with a {@link MethodParameter} and an optional cause. */ public ServerErrorException(String reason, MethodParameter parameter, @Nullable Throwable cause) { super(HttpStatus.INTERNAL_SERVER_ERROR, reason, cause); - this.handlerMethod = null; + this.handlerMethod = parameter.getMethod(); this.parameter = parameter; } - /** - * Constructor for a 500 error with a root cause. - */ - public ServerErrorException(String reason, HandlerMethod handlerMethod, @Nullable Throwable cause) { - super(HttpStatus.INTERNAL_SERVER_ERROR, reason, cause); - this.handlerMethod = handlerMethod; - this.parameter = null; - } - /** * Constructor for a 500 error linked to a specific {@code MethodParameter}. * @deprecated in favor of {@link #ServerErrorException(String, MethodParameter, Throwable)} @@ -85,18 +78,29 @@ public class ServerErrorException extends ResponseStatusException { this(reason, parameter, null); } + /** + * Constructor for a 500 error with a reason only. + * @deprecated in favor of {@link #ServerErrorException(String, Throwable)} + */ + @Deprecated + public ServerErrorException(String reason) { + super(HttpStatus.INTERNAL_SERVER_ERROR, reason, null); + this.handlerMethod = null; + this.parameter = null; + } + /** - * Return the controller method associated with the error, if any. + * Return the handler method associated with the error, if any. * @since 5.0.5 */ @Nullable - public HandlerMethod getHandlerMethod() { + public Method getHandlerMethod() { return this.handlerMethod; } /** - * Return the controller method argument associated with this error, if any. + * Return the specific method parameter associated with the error, if any. */ @Nullable public MethodParameter getMethodParameter() { diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/SyncInvocableHandlerMethod.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/SyncInvocableHandlerMethod.java index c60f24b88b5..afd59abc7d0 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/SyncInvocableHandlerMethod.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/SyncInvocableHandlerMethod.java @@ -109,7 +109,7 @@ public class SyncInvocableHandlerMethod extends HandlerMethod { Throwable ex = processor.getError(); if (ex != null) { throw (ex instanceof ServerErrorException ? (ServerErrorException) ex : - new ServerErrorException("Failed to invoke: " + getShortLogMessage(), this, ex)); + new ServerErrorException("Failed to invoke: " + getShortLogMessage(), getMethod(), ex)); } return processor.peek(); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/MatrixVariableMethodArgumentResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/MatrixVariableMethodArgumentResolver.java index a2824b4c859..0e999c84d3e 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/MatrixVariableMethodArgumentResolver.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/MatrixVariableMethodArgumentResolver.java @@ -98,7 +98,8 @@ public class MatrixVariableMethodArgumentResolver extends AbstractNamedValueSync String paramType = param.getNestedParameterType().getName(); throw new ServerErrorException( "Found more than one match for URI path parameter '" + name + - "' for parameter type [" + paramType + "]. Use 'pathVar' attribute to disambiguate."); + "' for parameter type [" + paramType + "]. Use 'pathVar' attribute to disambiguate.", + param, null); } paramValues.addAll(params.get(name)); found = true;