Skip to content

Commit

Permalink
Add MessageSource getters
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Nov 28, 2022
1 parent 8e9a553 commit 6c8fb6c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
Expand Up @@ -268,6 +268,19 @@ interface Builder {
*/
ErrorResponse build();

/**
* Build the {@code ErrorResponse} instance and also resolve the "detail"
* and "title" through the given {@link MessageSource}. Effectively a
* shortcut for calling {@link #build()} and then
* {@link ErrorResponse#updateAndGetBody(MessageSource, Locale)}.
* @since 6.0.3
*/
default ErrorResponse build(@Nullable MessageSource messageSource, Locale locale) {
ErrorResponse response = build();
response.updateAndGetBody(messageSource, locale);
return response;
}

}

}
Expand Up @@ -26,6 +26,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ProblemDetail;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
Expand Down Expand Up @@ -126,12 +127,12 @@ public static List<String> errorsToStringList(List<? extends ObjectError> errors
* back on the error's default message.
* @since 6.0
*/
@SuppressWarnings("ConstantConditions")
public static List<String> errorsToStringList(
List<? extends ObjectError> errors, MessageSource source, Locale locale) {
List<? extends ObjectError> errors, @Nullable MessageSource source, Locale locale) {

return errorsToStringList(errors, error -> source.getMessage(
error.getCode(), error.getArguments(), error.getDefaultMessage(), locale));
return (source != null ?
errorsToStringList(errors, error -> source.getMessage(error, locale)) :
errorsToStringList(errors));
}

private static List<String> errorsToStringList(
Expand Down
Expand Up @@ -75,6 +75,11 @@ public void setMessageSource(MessageSource messageSource) {
this.messageSource = messageSource;
}

@Nullable
public MessageSource getMessageSource() {
return this.messageSource;
}


/**
* Handle all exceptions raised within Spring MVC handling of the request .
Expand Down
Expand Up @@ -96,6 +96,11 @@ public void setMessageSource(MessageSource messageSource) {
this.messageSource = messageSource;
}

@Nullable
protected MessageSource getMessageSource() {
return this.messageSource;
}


/**
* Handle all exceptions raised within Spring MVC handling of the request .
Expand Down

0 comments on commit 6c8fb6c

Please sign in to comment.