Browse Source

Minor refactoring after recent commits

See gh-29384
pull/29412/head
rstoyanchev 3 years ago
parent
commit
921eeadff4
  1. 9
      spring-web/src/main/java/org/springframework/web/ErrorResponse.java
  2. 7
      spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityExceptionHandler.java
  3. 2
      spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityExceptionHandlerTests.java
  4. 15
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandler.java

9
spring-web/src/main/java/org/springframework/web/ErrorResponse.java

@ -100,14 +100,15 @@ public interface ErrorResponse { @@ -100,14 +100,15 @@ public interface ErrorResponse {
* <p>By default this is initialized via
* {@link #getDefaultDetailMessageCode(Class, String)}.
*/
default String getTitleCode() {
default String getTitleMessageCode() {
return getDefaultTitleMessageCode(getClass());
}
/**
* Resolve the {@link #getDetailMessageCode() detailMessageCode} and the
* {@link #getTitleCode() titleCode} through the given {@link MessageSource},
* and if found, update the "detail" and "title!" fields respectively.
* {@link #getTitleMessageCode() titleCode} through the given
* {@link MessageSource}, and if found, update the "detail" and "title!"
* fields respectively.
* @param messageSource the {@code MessageSource} to use for the lookup
* @param locale the {@code Locale} to use for the lookup
*/
@ -118,7 +119,7 @@ public interface ErrorResponse { @@ -118,7 +119,7 @@ public interface ErrorResponse {
if (detail != null) {
getBody().setDetail(detail);
}
String title = messageSource.getMessage(getTitleCode(), null, null, locale);
String title = messageSource.getMessage(getTitleMessageCode(), null, null, locale);
if (title != null) {
getBody().setTitle(title);
}

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

@ -306,12 +306,11 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa @@ -306,12 +306,11 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa
* @return the created {@code ProblemDetail} instance
*/
protected ProblemDetail createProblemDetail(
Exception ex, HttpStatusCode status, @Nullable HttpHeaders headers,
String defaultDetail, @Nullable String detailMessageCode, @Nullable Object[] detailMessageArguments,
ServerWebExchange exchange) {
Exception ex, HttpStatusCode status, String defaultDetail, @Nullable String detailMessageCode,
@Nullable Object[] detailMessageArguments, ServerWebExchange exchange) {
ErrorResponse response = ErrorResponse.createFor(
ex, status, headers, defaultDetail, detailMessageCode, detailMessageArguments);
ex, status, null, defaultDetail, detailMessageCode, detailMessageArguments);
return response.updateAndGetBody(this.messageSource, getLocale(exchange));
}

2
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityExceptionHandlerTests.java

@ -279,7 +279,7 @@ public class ResponseEntityExceptionHandlerTests { @@ -279,7 +279,7 @@ public class ResponseEntityExceptionHandlerTests {
public Mono<ResponseEntity<Object>> handleException(IllegalStateException ex, ServerWebExchange exchange) {
HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
ProblemDetail body = createProblemDetail(ex, status, null, ex.getMessage(), null, new Object[] {"A"}, exchange);
ProblemDetail body = createProblemDetail(ex, status, ex.getMessage(), null, new Object[] {"A"}, exchange);
return handleExceptionInternal(ex, body, null, status, exchange);
}
}

15
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandler.java

@ -394,7 +394,7 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa @@ -394,7 +394,7 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa
Object[] args = {ex.getPropertyName(), ex.getValue()};
String defaultDetail = "Failed to convert '" + args[0] + "' with value: '" + args[1] + "'";
ProblemDetail body = createProblemDetail(ex, status, headers, defaultDetail, null, args, request);
ProblemDetail body = createProblemDetail(ex, status, defaultDetail, null, args, request);
return handleExceptionInternal(ex, body, headers, status, request);
}
@ -419,7 +419,7 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa @@ -419,7 +419,7 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa
Object[] args = {ex.getPropertyName(), ex.getValue()};
String defaultDetail = "Failed to convert '" + args[0] + "' with value: '" + args[1] + "'";
String messageCode = ErrorResponse.getDefaultDetailMessageCode(TypeMismatchException.class, null);
ProblemDetail body = createProblemDetail(ex, status, headers, defaultDetail, messageCode, args, request);
ProblemDetail body = createProblemDetail(ex, status, defaultDetail, messageCode, args, request);
return handleExceptionInternal(ex, body, headers, status, request);
}
@ -441,7 +441,7 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa @@ -441,7 +441,7 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa
protected ResponseEntity<Object> handleHttpMessageNotReadable(
HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
ProblemDetail body = createProblemDetail(ex, status, headers, "Failed to read request", null, null, request);
ProblemDetail body = createProblemDetail(ex, status, "Failed to read request", null, null, request);
return handleExceptionInternal(ex, body, headers, status, request);
}
@ -462,7 +462,7 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa @@ -462,7 +462,7 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa
protected ResponseEntity<Object> handleHttpMessageNotWritable(
HttpMessageNotWritableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
ProblemDetail body = createProblemDetail(ex, status, headers, "Failed to write request", null, null, request);
ProblemDetail body = createProblemDetail(ex, status, "Failed to write request", null, null, request);
return handleExceptionInternal(ex, body, headers, status, request);
}
@ -505,12 +505,11 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa @@ -505,12 +505,11 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa
* @since 6.0
*/
protected ProblemDetail createProblemDetail(
Exception ex, HttpStatusCode status, @Nullable HttpHeaders headers,
String defaultDetail, @Nullable String detailMessageCode, @Nullable Object[] detailMessageArguments,
WebRequest request) {
Exception ex, HttpStatusCode status, String defaultDetail, @Nullable String detailMessageCode,
@Nullable Object[] detailMessageArguments, WebRequest request) {
ErrorResponse errorResponse = ErrorResponse.createFor(
ex, status, headers, defaultDetail, detailMessageCode, detailMessageArguments);
ex, status, null, defaultDetail, detailMessageCode, detailMessageArguments);
return errorResponse.updateAndGetBody(this.messageSource, LocaleContextHolder.getLocale());
}

Loading…
Cancel
Save