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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 7 additions & 4 deletions
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

Lines changed: 9 additions & 9 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 3 additions & 7 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)