Skip to content

Commit b045e5b

Browse files
committed
Tests for ErrorResponse hierarchy to verify the output
See gh-27052
1 parent 679432e commit b045e5b

20 files changed

+400
-42
lines changed

spring-web/src/main/java/org/springframework/web/ErrorResponseException.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public ErrorResponseException(HttpStatus status) {
6161
* Constructor with a well-known {@link HttpStatus} and an optional cause.
6262
*/
6363
public ErrorResponseException(HttpStatus status, @Nullable Throwable cause) {
64-
this(status.value(), null);
64+
this(status.value(), cause);
6565
}
6666

6767
/**

spring-web/src/main/java/org/springframework/web/HttpMediaTypeNotAcceptableException.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.web;
1818

1919
import java.util.List;
20+
import java.util.stream.Collectors;
2021

2122
import org.springframework.http.HttpHeaders;
2223
import org.springframework.http.HttpStatus;
@@ -39,15 +40,17 @@ public class HttpMediaTypeNotAcceptableException extends HttpMediaTypeException
3940
*/
4041
public HttpMediaTypeNotAcceptableException(String message) {
4142
super(message);
42-
getBody().setDetail("Could not parse Accept header");
43+
getBody().setDetail("Could not parse Accept header.");
4344
}
4445

4546
/**
4647
* Create a new HttpMediaTypeNotSupportedException.
47-
* @param supportedMediaTypes the list of supported media types
48+
* @param mediaTypes the list of supported media types
4849
*/
49-
public HttpMediaTypeNotAcceptableException(List<MediaType> supportedMediaTypes) {
50-
super("No acceptable representation", supportedMediaTypes);
50+
public HttpMediaTypeNotAcceptableException(List<MediaType> mediaTypes) {
51+
super("No acceptable representation", mediaTypes);
52+
getBody().setDetail("Acceptable representations: " +
53+
mediaTypes.stream().map(MediaType::toString).collect(Collectors.joining(", ", "'", "'")) + ".");
5154
}
5255

5356

spring-web/src/main/java/org/springframework/web/HttpMediaTypeNotSupportedException.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,29 @@ public HttpMediaTypeNotSupportedException(String message) {
5151
super(message);
5252
this.contentType = null;
5353
this.httpMethod = null;
54-
getBody().setDetail("Could not parse Content-Type");
54+
getBody().setDetail("Could not parse Content-Type.");
5555
}
5656

5757
/**
5858
* Create a new HttpMediaTypeNotSupportedException.
5959
* @param contentType the unsupported content type
60-
* @param supportedMediaTypes the list of supported media types
60+
* @param mediaTypes the list of supported media types
6161
*/
62-
public HttpMediaTypeNotSupportedException(@Nullable MediaType contentType, List<MediaType> supportedMediaTypes) {
63-
this(contentType, supportedMediaTypes, null);
62+
public HttpMediaTypeNotSupportedException(@Nullable MediaType contentType, List<MediaType> mediaTypes) {
63+
this(contentType, mediaTypes, null);
6464
}
6565

6666
/**
6767
* Create a new HttpMediaTypeNotSupportedException.
6868
* @param contentType the unsupported content type
69-
* @param supportedMediaTypes the list of supported media types
69+
* @param mediaTypes the list of supported media types
7070
* @param httpMethod the HTTP method of the request
7171
* @since 6.0
7272
*/
73-
public HttpMediaTypeNotSupportedException(@Nullable MediaType contentType,
74-
List<MediaType> supportedMediaTypes, @Nullable HttpMethod httpMethod) {
73+
public HttpMediaTypeNotSupportedException(
74+
@Nullable MediaType contentType, List<MediaType> mediaTypes, @Nullable HttpMethod httpMethod) {
7575

76-
this(contentType, supportedMediaTypes, httpMethod,
76+
this(contentType, mediaTypes, httpMethod,
7777
"Content-Type " + (contentType != null ? "'" + contentType + "' " : "") + "is not supported");
7878
}
7979

@@ -91,7 +91,7 @@ public HttpMediaTypeNotSupportedException(@Nullable MediaType contentType,
9191
super(message, supportedMediaTypes);
9292
this.contentType = contentType;
9393
this.httpMethod = httpMethod;
94-
getBody().setDetail("Content-Type " + this.contentType + " is not supported");
94+
getBody().setDetail("Content-Type '" + this.contentType + "' is not supported.");
9595
}
9696

9797

spring-web/src/main/java/org/springframework/web/HttpRequestMethodNotSupportedException.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ public HttpRequestMethodNotSupportedException(String method, @Nullable String[]
9393
super(msg);
9494
this.method = method;
9595
this.supportedMethods = supportedMethods;
96-
this.body = ProblemDetail.forRawStatusCode(getRawStatusCode())
97-
.withDetail("Method '" + method + "' is not supported");
96+
97+
String detail = "Method '" + method + "' is not supported.";
98+
this.body = ProblemDetail.forRawStatusCode(getRawStatusCode()).withDetail(detail);
9899
}
99100

100101

spring-web/src/main/java/org/springframework/web/bind/MethodArgumentNotValidException.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class MethodArgumentNotValidException extends BindException implements Er
4848
public MethodArgumentNotValidException(MethodParameter parameter, BindingResult bindingResult) {
4949
super(bindingResult);
5050
this.parameter = parameter;
51-
this.body = ProblemDetail.forRawStatusCode(getRawStatusCode()).withDetail(initMessage(parameter));
51+
this.body = ProblemDetail.forRawStatusCode(getRawStatusCode()).withDetail("Invalid request content.");
5252
}
5353

5454

@@ -71,13 +71,9 @@ public final MethodParameter getParameter() {
7171

7272
@Override
7373
public String getMessage() {
74-
return initMessage(this.parameter);
75-
}
76-
77-
private String initMessage(MethodParameter parameter) {
7874
StringBuilder sb = new StringBuilder("Validation failed for argument [")
79-
.append(parameter.getParameterIndex()).append("] in ")
80-
.append(parameter.getExecutable().toGenericString());
75+
.append(this.parameter.getParameterIndex()).append("] in ")
76+
.append(this.parameter.getExecutable().toGenericString());
8177
BindingResult bindingResult = getBindingResult();
8278
if (bindingResult.getErrorCount() > 1) {
8379
sb.append(" with ").append(bindingResult.getErrorCount()).append(" errors");

spring-web/src/main/java/org/springframework/web/bind/MissingMatrixVariableException.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public MissingMatrixVariableException(
5757
super("", missingAfterConversion);
5858
this.variableName = variableName;
5959
this.parameter = parameter;
60-
getBody().setDetail("Required path parameter '" + this.variableName + "' is not present");
60+
getBody().setDetail("Required path parameter '" + this.variableName + "' is not present.");
6161
}
6262

6363

spring-web/src/main/java/org/springframework/web/bind/MissingPathVariableException.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public MissingPathVariableException(
6060
super("", missingAfterConversion);
6161
this.variableName = variableName;
6262
this.parameter = parameter;
63-
getBody().setDetail("Required URI variable '" + this.variableName + "' is not present");
63+
getBody().setDetail("Required path variable '" + this.variableName + "' is not present.");
6464
}
6565

6666

spring-web/src/main/java/org/springframework/web/bind/MissingRequestCookieException.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public MissingRequestCookieException(
5757
super("", missingAfterConversion);
5858
this.cookieName = cookieName;
5959
this.parameter = parameter;
60-
getBody().setDetail("Required cookie '" + this.cookieName + "' is not present");
60+
getBody().setDetail("Required cookie '" + this.cookieName + "' is not present.");
6161
}
6262

6363

spring-web/src/main/java/org/springframework/web/bind/MissingRequestHeaderException.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public MissingRequestHeaderException(
5757
super("", missingAfterConversion);
5858
this.headerName = headerName;
5959
this.parameter = parameter;
60-
getBody().setDetail("Required header '" + this.headerName + "' is not present");
60+
getBody().setDetail("Required header '" + this.headerName + "' is not present.");
6161
}
6262

6363

spring-web/src/main/java/org/springframework/web/bind/MissingServletRequestParameterException.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public MissingServletRequestParameterException(
5252
super("", missingAfterConversion);
5353
this.parameterName = parameterName;
5454
this.parameterType = parameterType;
55-
getBody().setDetail("Required parameter '" + this.parameterName + "' is not present");
55+
getBody().setDetail("Required parameter '" + this.parameterName + "' is not present.");
5656
}
5757

5858

spring-web/src/main/java/org/springframework/web/bind/UnsatisfiedServletRequestParameterException.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.web.bind;
1818

19-
import java.util.Arrays;
2019
import java.util.Iterator;
2120
import java.util.List;
2221
import java.util.Map;
@@ -48,9 +47,7 @@ public class UnsatisfiedServletRequestParameterException extends ServletRequestB
4847
* @param actualParams the actual parameter Map associated with the ServletRequest
4948
*/
5049
public UnsatisfiedServletRequestParameterException(String[] paramConditions, Map<String, String[]> actualParams) {
51-
super("");
52-
this.paramConditions = Arrays.<String[]>asList(paramConditions);
53-
this.actualParams = actualParams;
50+
this(List.<String[]>of(paramConditions), actualParams);
5451
}
5552

5653
/**
@@ -66,6 +63,7 @@ public UnsatisfiedServletRequestParameterException(List<String[]> paramCondition
6663
Assert.notEmpty(paramConditions, "Parameter conditions must not be empty");
6764
this.paramConditions = paramConditions;
6865
this.actualParams = actualParams;
66+
getBody().setDetail("Invalid request parameters.");
6967
}
7068

7169

spring-web/src/main/java/org/springframework/web/bind/support/WebExchangeBindException.java

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class WebExchangeBindException extends ServerWebInputException implements
4949
public WebExchangeBindException(MethodParameter parameter, BindingResult bindingResult) {
5050
super("Validation failure", parameter);
5151
this.bindingResult = bindingResult;
52+
getBody().setDetail("Invalid request content.");
5253
}
5354

5455

spring-web/src/main/java/org/springframework/web/multipart/support/MissingServletRequestPartException.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class MissingServletRequestPartException extends ServletRequestBindingExc
4040
* @param requestPartName the name of the missing part of the multipart request
4141
*/
4242
public MissingServletRequestPartException(String requestPartName) {
43-
super("Required request part '" + requestPartName + "' is not present");
43+
super("Required part '" + requestPartName + "' is not present.");
4444
this.requestPartName = requestPartName;
4545
getBody().setDetail(getMessage());
4646
}

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Collections;
2121
import java.util.LinkedHashSet;
2222
import java.util.Set;
23+
import java.util.stream.Collectors;
2324

2425
import org.springframework.http.HttpHeaders;
2526
import org.springframework.http.HttpMethod;
@@ -47,13 +48,17 @@ public MethodNotAllowedException(HttpMethod method, Collection<HttpMethod> suppo
4748
}
4849

4950
public MethodNotAllowedException(String method, @Nullable Collection<HttpMethod> supportedMethods) {
50-
super(HttpStatus.METHOD_NOT_ALLOWED, "Request method '" + method + "' not supported");
51+
super(HttpStatus.METHOD_NOT_ALLOWED, "Request method '" + method + "' is not supported.");
5152
Assert.notNull(method, "'method' is required");
5253
if (supportedMethods == null) {
5354
supportedMethods = Collections.emptySet();
5455
}
5556
this.method = method;
5657
this.httpMethods = Collections.unmodifiableSet(new LinkedHashSet<>(supportedMethods));
58+
59+
getBody().setDetail(this.httpMethods.isEmpty() ? getReason() :
60+
"Supported methods: " + this.httpMethods.stream()
61+
.map(HttpMethod::toString).collect(Collectors.joining("', '", "'", "'")));
5762
}
5863

5964

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Collections;
2020
import java.util.List;
21+
import java.util.stream.Collectors;
2122

2223
import org.springframework.http.HttpHeaders;
2324
import org.springframework.http.HttpStatus;
@@ -42,14 +43,17 @@ public class NotAcceptableStatusException extends ResponseStatusException {
4243
public NotAcceptableStatusException(String reason) {
4344
super(HttpStatus.NOT_ACCEPTABLE, reason);
4445
this.supportedMediaTypes = Collections.emptyList();
46+
getBody().setDetail("Could not parse Accept header.");
4547
}
4648

4749
/**
4850
* Constructor for when the requested Content-Type is not supported.
4951
*/
50-
public NotAcceptableStatusException(List<MediaType> supportedMediaTypes) {
52+
public NotAcceptableStatusException(List<MediaType> mediaTypes) {
5153
super(HttpStatus.NOT_ACCEPTABLE, "Could not find acceptable representation");
52-
this.supportedMediaTypes = Collections.unmodifiableList(supportedMediaTypes);
54+
this.supportedMediaTypes = Collections.unmodifiableList(mediaTypes);
55+
getBody().setDetail("Acceptable representations: " +
56+
mediaTypes.stream().map(MediaType::toString).collect(Collectors.joining(", ", "'", "'")) + ".");
5357
}
5458

5559

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

-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ public ResponseStatusException(HttpStatus status, @Nullable String reason, @Null
7777
public ResponseStatusException(int rawStatusCode, @Nullable String reason, @Nullable Throwable cause) {
7878
super(rawStatusCode, cause);
7979
this.reason = reason;
80-
setDetail(reason);
8180
}
8281

8382

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public UnsupportedMediaTypeStatusException(@Nullable String reason) {
5757
this.supportedMediaTypes = Collections.emptyList();
5858
this.bodyType = null;
5959
this.method = null;
60+
getBody().setDetail("Could not parse Content-Type.");
6061
}
6162

6263
/**
@@ -100,8 +101,7 @@ public UnsupportedMediaTypeStatusException(@Nullable MediaType contentType, List
100101
this.bodyType = bodyType;
101102
this.method = method;
102103

103-
// Set explicitly to avoid implementation details
104-
setDetail(contentType != null ? "Content-Type '" + contentType + "' is not supported" : null);
104+
setDetail(contentType != null ? "Content-Type '" + contentType + "' is not supported." : null);
105105
}
106106

107107

0 commit comments

Comments
 (0)