Skip to content

Commit

Permalink
Minor refactoring after recent commits
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Nov 1, 2022
1 parent e71057d commit 921eead
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,15 @@ default Object[] getDetailMessageArguments(MessageSource messageSource, Locale l
* <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
*/
Expand All @@ -118,7 +119,7 @@ default ProblemDetail updateAndGetBody(@Nullable MessageSource messageSource, Lo
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,11 @@ protected Mono<ResponseEntity<Object>> handleErrorResponseException(
* @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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ protected Mono<ResponseEntity<Object>> handleErrorResponseException(

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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ protected ResponseEntity<Object> handleConversionNotSupported(

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);
}
Expand All @@ -419,7 +419,7 @@ protected ResponseEntity<Object> handleTypeMismatch(
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);
}
Expand All @@ -441,7 +441,7 @@ protected ResponseEntity<Object> handleTypeMismatch(
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);
}

Expand All @@ -462,7 +462,7 @@ protected ResponseEntity<Object> handleHttpMessageNotReadable(
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);
}

Expand Down Expand Up @@ -505,12 +505,11 @@ protected ResponseEntity<Object> handleBindException(
* @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());
}
Expand Down

0 comments on commit 921eead

Please sign in to comment.