Skip to content

Commit 66a3a82

Browse files
committed
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).
1 parent 97735e4 commit 66a3a82

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

spring-web/src/main/java/org/springframework/web/server/ServerErrorException.java

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616

1717
package org.springframework.web.server;
1818

19+
import java.lang.reflect.Method;
20+
1921
import org.springframework.core.MethodParameter;
2022
import org.springframework.http.HttpStatus;
2123
import org.springframework.lang.Nullable;
22-
import org.springframework.web.method.HandlerMethod;
2324

2425
/**
2526
* Exception for an {@link HttpStatus#INTERNAL_SERVER_ERROR} that exposes extra
@@ -33,49 +34,41 @@
3334
public class ServerErrorException extends ResponseStatusException {
3435

3536
@Nullable
36-
private final HandlerMethod handlerMethod;
37+
private final Method handlerMethod;
3738

3839
@Nullable
3940
private final MethodParameter parameter;
4041

4142

4243
/**
43-
* Constructor with an explanation only.
44+
* Constructor for a 500 error with a reason and an optional cause.
45+
* @since 5.0.5
4446
*/
45-
public ServerErrorException(String reason) {
46-
super(HttpStatus.INTERNAL_SERVER_ERROR, reason, null);
47+
public ServerErrorException(String reason, @Nullable Throwable cause) {
48+
super(HttpStatus.INTERNAL_SERVER_ERROR, reason, cause);
4749
this.handlerMethod = null;
4850
this.parameter = null;
4951
}
5052

5153
/**
52-
* Constructor with a reason and root cause.
54+
* Constructor for a 500 error with a handler {@link Method} and an optional cause.
5355
* @since 5.0.5
5456
*/
55-
public ServerErrorException(String reason, Throwable cause) {
57+
public ServerErrorException(String reason, Method handlerMethod, @Nullable Throwable cause) {
5658
super(HttpStatus.INTERNAL_SERVER_ERROR, reason, cause);
57-
this.handlerMethod = null;
59+
this.handlerMethod = handlerMethod;
5860
this.parameter = null;
5961
}
6062

6163
/**
62-
* Constructor for a 500 error with a {@link MethodParameter}.
64+
* Constructor for a 500 error with a {@link MethodParameter} and an optional cause.
6365
*/
6466
public ServerErrorException(String reason, MethodParameter parameter, @Nullable Throwable cause) {
6567
super(HttpStatus.INTERNAL_SERVER_ERROR, reason, cause);
66-
this.handlerMethod = null;
68+
this.handlerMethod = parameter.getMethod();
6769
this.parameter = parameter;
6870
}
6971

70-
/**
71-
* Constructor for a 500 error with a root cause.
72-
*/
73-
public ServerErrorException(String reason, HandlerMethod handlerMethod, @Nullable Throwable cause) {
74-
super(HttpStatus.INTERNAL_SERVER_ERROR, reason, cause);
75-
this.handlerMethod = handlerMethod;
76-
this.parameter = null;
77-
}
78-
7972
/**
8073
* Constructor for a 500 error linked to a specific {@code MethodParameter}.
8174
* @deprecated in favor of {@link #ServerErrorException(String, MethodParameter, Throwable)}
@@ -85,18 +78,29 @@ public ServerErrorException(String reason, MethodParameter parameter) {
8578
this(reason, parameter, null);
8679
}
8780

81+
/**
82+
* Constructor for a 500 error with a reason only.
83+
* @deprecated in favor of {@link #ServerErrorException(String, Throwable)}
84+
*/
85+
@Deprecated
86+
public ServerErrorException(String reason) {
87+
super(HttpStatus.INTERNAL_SERVER_ERROR, reason, null);
88+
this.handlerMethod = null;
89+
this.parameter = null;
90+
}
91+
8892

8993
/**
90-
* Return the controller method associated with the error, if any.
94+
* Return the handler method associated with the error, if any.
9195
* @since 5.0.5
9296
*/
9397
@Nullable
94-
public HandlerMethod getHandlerMethod() {
98+
public Method getHandlerMethod() {
9599
return this.handlerMethod;
96100
}
97101

98102
/**
99-
* Return the controller method argument associated with this error, if any.
103+
* Return the specific method parameter associated with the error, if any.
100104
*/
101105
@Nullable
102106
public MethodParameter getMethodParameter() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public HandlerResult invokeForHandlerResult(ServerWebExchange exchange,
109109
Throwable ex = processor.getError();
110110
if (ex != null) {
111111
throw (ex instanceof ServerErrorException ? (ServerErrorException) ex :
112-
new ServerErrorException("Failed to invoke: " + getShortLogMessage(), this, ex));
112+
new ServerErrorException("Failed to invoke: " + getShortLogMessage(), getMethod(), ex));
113113
}
114114
return processor.peek();
115115
}

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/MatrixVariableMethodArgumentResolver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ protected Object resolveNamedValue(String name, MethodParameter param, ServerWeb
9898
String paramType = param.getNestedParameterType().getName();
9999
throw new ServerErrorException(
100100
"Found more than one match for URI path parameter '" + name +
101-
"' for parameter type [" + paramType + "]. Use 'pathVar' attribute to disambiguate.");
101+
"' for parameter type [" + paramType + "]. Use 'pathVar' attribute to disambiguate.",
102+
param, null);
102103
}
103104
paramValues.addAll(params.get(name));
104105
found = true;

0 commit comments

Comments
 (0)