Add "title" message code to ErrorResponse.Builder

See gh-30566
This commit is contained in:
rstoyanchev 2023-06-14 16:22:56 +01:00
parent 61eaa9333b
commit 48861b67dd
2 changed files with 38 additions and 12 deletions

View File

@ -50,6 +50,8 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder {
private String titleMessageCode;
private String typeMessageCode;
DefaultErrorResponseBuilder(Throwable ex, HttpStatusCode statusCode, String detail) {
Assert.notNull(ex, "Throwable is required");
@ -60,6 +62,7 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder {
this.problemDetail = ProblemDetail.forStatusAndDetail(statusCode, detail);
this.detailMessageCode = ErrorResponse.getDefaultDetailMessageCode(ex.getClass(), null);
this.titleMessageCode = ErrorResponse.getDefaultTitleMessageCode(ex.getClass());
this.typeMessageCode = ErrorResponse.getDefaultTypeMessageCode(ex.getClass());
}
@ -102,6 +105,12 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder {
return this;
}
@Override
public ErrorResponse.Builder typeMessageCode(String messageCode) {
this.typeMessageCode = messageCode;
return this;
}
@Override
public ErrorResponse.Builder title(@Nullable String title) {
this.problemDetail.setTitle(title);
@ -131,7 +140,8 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder {
public ErrorResponse build() {
return new SimpleErrorResponse(
this.exception, this.statusCode, this.headers, this.problemDetail,
this.detailMessageCode, this.detailMessageArguments, this.titleMessageCode);
this.detailMessageCode, this.detailMessageArguments, this.titleMessageCode,
this.typeMessageCode);
}
@ -155,9 +165,12 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder {
private final String titleMessageCode;
private final String typeMessageCode;
SimpleErrorResponse(
Throwable ex, HttpStatusCode statusCode, @Nullable HttpHeaders headers, ProblemDetail problemDetail,
String detailMessageCode, @Nullable Object[] detailMessageArguments, String titleMessageCode) {
String detailMessageCode, @Nullable Object[] detailMessageArguments, String titleMessageCode,
String typeMessageCode) {
this.exception = ex;
this.statusCode = statusCode;
@ -166,6 +179,7 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder {
this.detailMessageCode = detailMessageCode;
this.detailMessageArguments = detailMessageArguments;
this.titleMessageCode = titleMessageCode;
this.typeMessageCode = typeMessageCode;
}
@Override
@ -198,6 +212,11 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder {
return this.titleMessageCode;
}
@Override
public String getTypeMessageCode() {
return this.typeMessageCode;
}
@Override
public String toString() {
return "ErrorResponse{status=" + this.statusCode + ", " +

View File

@ -239,6 +239,17 @@ public interface ErrorResponse {
*/
Builder type(URI type);
/**
* Customize the {@link MessageSource} code to use to resolve the value
* for {@link ProblemDetail#setType(URI)}.
* <p>By default, set from {@link ErrorResponse#getDefaultTypeMessageCode(Class)}.
* @param messageCode the message code to use
* @return the same builder instance
* @since 6.1
* @see ErrorResponse#getTypeMessageCode()
*/
Builder typeMessageCode(String messageCode);
/**
* Set the underlying {@link ProblemDetail#setTitle(String) title} field.
* @return the same builder instance
@ -246,11 +257,9 @@ public interface ErrorResponse {
Builder title(@Nullable String title);
/**
* Customize the {@link MessageSource} code for looking up the value for
* the underlying {@link ProblemDetail#setTitle(String) title}.
* <p>By default, set via
* {@link ErrorResponse#getDefaultTitleMessageCode(Class)} with the
* associated Exception type.
* Customize the {@link MessageSource} code to use to resolve the value
* for {@link ProblemDetail#setTitle(String)}.
* <p>By default, set from {@link ErrorResponse#getDefaultTitleMessageCode(Class)}.
* @param messageCode the message code to use
* @return the same builder instance
* @see ErrorResponse#getTitleMessageCode()
@ -270,11 +279,9 @@ public interface ErrorResponse {
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.
* Customize the {@link MessageSource} code to use to resolve the value
* for the {@link #detail(String)}.
* <p>By default, set from {@link ErrorResponse#getDefaultDetailMessageCode(Class, String)}.
* @param messageCode the message code to use
* @return the same builder instance
* @see ErrorResponse#getDetailMessageCode()