Polishing in ErrorResponse

See gh-30566
This commit is contained in:
rstoyanchev
2023-06-14 16:13:21 +01:00
parent 9c7b5cb3f5
commit 61eaa9333b
3 changed files with 81 additions and 81 deletions

View File

@@ -64,6 +64,9 @@ public interface ErrorResponse {
*/ */
ProblemDetail getBody(); ProblemDetail getBody();
// MessageSource codes and arguments
/** /**
* Return a code to use to resolve the problem "type" for this exception * Return a code to use to resolve the problem "type" for this exception
* through a {@link MessageSource}. The type resolved through the * through a {@link MessageSource}. The type resolved through the
@@ -76,6 +79,15 @@ public interface ErrorResponse {
return getDefaultTypeMessageCode(getClass()); return getDefaultTypeMessageCode(getClass());
} }
/**
* Return a code to use to resolve the problem "title" for this exception
* through a {@link MessageSource}.
* <p>By default this is initialized via {@link #getDefaultTitleMessageCode(Class)}.
*/
default String getTitleMessageCode() {
return getDefaultTitleMessageCode(getClass());
}
/** /**
* Return a code to use to resolve the problem "detail" for this exception * Return a code to use to resolve the problem "detail" for this exception
* through a {@link MessageSource}. * through a {@link MessageSource}.
@@ -111,15 +123,6 @@ public interface ErrorResponse {
return getDetailMessageArguments(); return getDetailMessageArguments();
} }
/**
* Return a code to use to resolve the problem "title" for this exception
* through a {@link MessageSource}.
* <p>By default this is initialized via {@link #getDefaultTitleMessageCode(Class)}.
*/
default String getTitleMessageCode() {
return getDefaultTitleMessageCode(getClass());
}
/** /**
* Use the given {@link MessageSource} to resolve the * Use the given {@link MessageSource} to resolve the
* {@link #getTypeMessageCode() type}, {@link #getTitleMessageCode() title}, * {@link #getTypeMessageCode() type}, {@link #getTitleMessageCode() title},
@@ -159,6 +162,16 @@ public interface ErrorResponse {
return "problemDetail.type." + exceptionType.getName(); return "problemDetail.type." + exceptionType.getName();
} }
/**
* Build a message code for the "title" field, for the given exception type.
* @param exceptionType the exception type associated with the problem
* @return {@code "problemDetail.title."} followed by the fully qualified
* {@link Class#getName() class name}
*/
static String getDefaultTitleMessageCode(Class<?> exceptionType) {
return "problemDetail.title." + exceptionType.getName();
}
/** /**
* Build a message code for the "detail" field, for the given exception type. * Build a message code for the "detail" field, for the given exception type.
* @param exceptionType the exception type associated with the problem * @param exceptionType the exception type associated with the problem
@@ -171,15 +184,6 @@ public interface ErrorResponse {
return "problemDetail." + exceptionType.getName() + (suffix != null ? "." + suffix : ""); return "problemDetail." + exceptionType.getName() + (suffix != null ? "." + suffix : "");
} }
/**
* Build a message code for the "title" field, for the given exception type.
* @param exceptionType the exception type associated with the problem
* @return {@code "problemDetail.title."} followed by the fully qualified
* {@link Class#getName() class name}
*/
static String getDefaultTitleMessageCode(Class<?> exceptionType) {
return "problemDetail.title." + exceptionType.getName();
}
/** /**
* Static factory method to build an instance via * Static factory method to build an instance via
@@ -229,33 +233,6 @@ public interface ErrorResponse {
*/ */
Builder headers(Consumer<HttpHeaders> headersConsumer); Builder headers(Consumer<HttpHeaders> headersConsumer);
/**
* Set the underlying {@link ProblemDetail#setDetail(String) detail}.
* @return the same builder instance
*/
Builder detail(String detail);
/**
* Customize the {@link MessageSource} code for looking up the value for
* the underlying {@link #detail(String) detail}.
* <p>By default, this is set to
* {@link ErrorResponse#getDefaultDetailMessageCode(Class, String)} with the
* associated Exception type.
* @param messageCode the message code to use
* @return the same builder instance
* @see ErrorResponse#getDetailMessageCode()
*/
Builder detailMessageCode(String messageCode);
/**
* Set the arguments to provide to the {@link MessageSource} lookup for
* {@link #detailMessageCode(String)}.
* @param messageArguments the arguments to provide
* @return the same builder instance
* @see ErrorResponse#getDetailMessageArguments()
*/
Builder detailMessageArguments(Object... messageArguments);
/** /**
* Set the underlying {@link ProblemDetail#setType(URI) type} field. * Set the underlying {@link ProblemDetail#setType(URI) type} field.
* @return the same builder instance * @return the same builder instance
@@ -286,6 +263,33 @@ public interface ErrorResponse {
*/ */
Builder instance(@Nullable URI instance); Builder instance(@Nullable URI instance);
/**
* Set the underlying {@link ProblemDetail#setDetail(String) detail}.
* @return the same builder instance
*/
Builder detail(String detail);
/**
* Customize the {@link MessageSource} code for looking up the value for
* the underlying {@link #detail(String) detail}.
* <p>By default, this is set to
* {@link ErrorResponse#getDefaultDetailMessageCode(Class, String)} with the
* associated Exception type.
* @param messageCode the message code to use
* @return the same builder instance
* @see ErrorResponse#getDetailMessageCode()
*/
Builder detailMessageCode(String messageCode);
/**
* Set the arguments to provide to the {@link MessageSource} lookup for
* {@link #detailMessageCode(String)}.
* @param messageArguments the arguments to provide
* @return the same builder instance
* @see ErrorResponse#getDetailMessageArguments()
*/
Builder detailMessageArguments(Object... messageArguments);
/** /**
* Set a "dynamic" {@link ProblemDetail#setProperty(String, Object) * Set a "dynamic" {@link ProblemDetail#setProperty(String, Object)
* property} on the underlying {@code ProblemDetail}. * property} on the underlying {@code ProblemDetail}.

View File

@@ -127,37 +127,35 @@ public class ResponseEntityExceptionHandlerTests {
@Test @Test
void errorResponseProblemDetailViaMessageSource() { void errorResponseProblemDetailViaMessageSource() {
Locale locale = Locale.UK; Locale locale = Locale.UK;
LocaleContextHolder.setLocale(locale);
String type = "https://example.com/probs/unsupported-content"; String type = "https://example.com/probs/unsupported-content";
String title = "Media type is not valid or not supported"; String title = "Media type is not valid or not supported";
Class<UnsupportedMediaTypeStatusException> exceptionType = UnsupportedMediaTypeStatusException.class;
StaticMessageSource messageSource = new StaticMessageSource(); StaticMessageSource source = new StaticMessageSource();
messageSource.addMessage( source.addMessage(ErrorResponse.getDefaultTypeMessageCode(exceptionType), locale, type);
ErrorResponse.getDefaultDetailMessageCode(UnsupportedMediaTypeStatusException.class, null), locale, source.addMessage(ErrorResponse.getDefaultTitleMessageCode(exceptionType), locale, title);
source.addMessage(ErrorResponse.getDefaultDetailMessageCode(exceptionType, null), locale,
"Content-Type {0} not supported. Supported: {1}"); "Content-Type {0} not supported. Supported: {1}");
messageSource.addMessage(
ErrorResponse.getDefaultTitleMessageCode(UnsupportedMediaTypeStatusException.class), locale, title);
messageSource.addMessage(
ErrorResponse.getDefaultTypeMessageCode(UnsupportedMediaTypeStatusException.class), locale, type);
this.exceptionHandler.setMessageSource(messageSource); this.exceptionHandler.setMessageSource(source);
Exception ex = new UnsupportedMediaTypeStatusException(MediaType.APPLICATION_JSON, Exception ex = new UnsupportedMediaTypeStatusException(
List.of(MediaType.APPLICATION_ATOM_XML, MediaType.APPLICATION_XML)); MediaType.APPLICATION_JSON, List.of(MediaType.APPLICATION_ATOM_XML, MediaType.APPLICATION_XML));
MockServerWebExchange exchange = MockServerWebExchange.from( MockServerWebExchange exchange = MockServerWebExchange.from(
MockServerHttpRequest.get("/").acceptLanguageAsLocales(locale).build()); MockServerHttpRequest.get("/").acceptLanguageAsLocales(locale).build());
ResponseEntity<?> responseEntity = this.exceptionHandler.handleException(ex, exchange).block(); ResponseEntity<?> responseEntity = this.exceptionHandler.handleException(ex, exchange).block();
assertThat(responseEntity).isNotNull();
ProblemDetail body = (ProblemDetail) responseEntity.getBody(); ProblemDetail problem = (ProblemDetail) responseEntity.getBody();
assertThat(body.getDetail()).isEqualTo( assertThat(problem).isNotNull();
assertThat(problem.getType()).isEqualTo(URI.create(type));
assertThat(problem.getTitle()).isEqualTo(title);
assertThat(problem.getDetail()).isEqualTo(
"Content-Type application/json not supported. Supported: [application/atom+xml, application/xml]"); "Content-Type application/json not supported. Supported: [application/atom+xml, application/xml]");
assertThat(body.getTitle()).isEqualTo(title);
assertThat(body.getType()).isEqualTo(URI.create(type));
} }
@Test @Test

View File

@@ -161,33 +161,31 @@ public class ResponseEntityExceptionHandlerTests {
@Test @Test
public void errorResponseProblemDetailViaMessageSource() { public void errorResponseProblemDetailViaMessageSource() {
Locale locale = Locale.UK;
LocaleContextHolder.setLocale(locale);
String type = "https://example.com/probs/unsupported-content";
String title = "Media type is not valid or not supported";
try { try {
StaticMessageSource messageSource = new StaticMessageSource(); Locale locale = Locale.UK;
messageSource.addMessage( LocaleContextHolder.setLocale(locale);
ErrorResponse.getDefaultDetailMessageCode(HttpMediaTypeNotSupportedException.class, null), locale,
"Content-Type {0} not supported. Supported: {1}");
messageSource.addMessage(
ErrorResponse.getDefaultTitleMessageCode(HttpMediaTypeNotSupportedException.class), locale, title);
messageSource.addMessage(
ErrorResponse.getDefaultTypeMessageCode(HttpMediaTypeNotSupportedException.class), locale, type);
this.exceptionHandler.setMessageSource(messageSource); String type = "https://example.com/probs/unsupported-content";
String title = "Media type is not valid or not supported";
Class<HttpMediaTypeNotSupportedException> exceptionType = HttpMediaTypeNotSupportedException.class;
StaticMessageSource source = new StaticMessageSource();
source.addMessage(ErrorResponse.getDefaultTypeMessageCode(exceptionType), locale, type);
source.addMessage(ErrorResponse.getDefaultTitleMessageCode(exceptionType), locale, title);
source.addMessage(ErrorResponse.getDefaultDetailMessageCode(exceptionType, null), locale,
"Content-Type {0} not supported. Supported: {1}");
this.exceptionHandler.setMessageSource(source);
ResponseEntity<?> entity = testException(new HttpMediaTypeNotSupportedException( ResponseEntity<?> entity = testException(new HttpMediaTypeNotSupportedException(
MediaType.APPLICATION_JSON, List.of(MediaType.APPLICATION_ATOM_XML, MediaType.APPLICATION_XML))); MediaType.APPLICATION_JSON, List.of(MediaType.APPLICATION_ATOM_XML, MediaType.APPLICATION_XML)));
ProblemDetail body = (ProblemDetail) entity.getBody(); ProblemDetail problem = (ProblemDetail) entity.getBody();
assertThat(body.getDetail()).isEqualTo( assertThat(problem).isNotNull();
assertThat(problem.getType()).isEqualTo(URI.create(type));
assertThat(problem.getTitle()).isEqualTo(title);
assertThat(problem.getDetail()).isEqualTo(
"Content-Type application/json not supported. Supported: [application/atom+xml, application/xml]"); "Content-Type application/json not supported. Supported: [application/atom+xml, application/xml]");
assertThat(body.getTitle()).isEqualTo(title);
assertThat(body.getType()).isEqualTo(URI.create(type));
} }
finally { finally {
LocaleContextHolder.resetLocaleContext(); LocaleContextHolder.resetLocaleContext();