Add "title" message code to ErrorResponse.Builder
See gh-30566
This commit is contained in:
parent
61eaa9333b
commit
48861b67dd
|
|
@ -50,6 +50,8 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder {
|
||||||
|
|
||||||
private String titleMessageCode;
|
private String titleMessageCode;
|
||||||
|
|
||||||
|
private String typeMessageCode;
|
||||||
|
|
||||||
|
|
||||||
DefaultErrorResponseBuilder(Throwable ex, HttpStatusCode statusCode, String detail) {
|
DefaultErrorResponseBuilder(Throwable ex, HttpStatusCode statusCode, String detail) {
|
||||||
Assert.notNull(ex, "Throwable is required");
|
Assert.notNull(ex, "Throwable is required");
|
||||||
|
|
@ -60,6 +62,7 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder {
|
||||||
this.problemDetail = ProblemDetail.forStatusAndDetail(statusCode, detail);
|
this.problemDetail = ProblemDetail.forStatusAndDetail(statusCode, detail);
|
||||||
this.detailMessageCode = ErrorResponse.getDefaultDetailMessageCode(ex.getClass(), null);
|
this.detailMessageCode = ErrorResponse.getDefaultDetailMessageCode(ex.getClass(), null);
|
||||||
this.titleMessageCode = ErrorResponse.getDefaultTitleMessageCode(ex.getClass());
|
this.titleMessageCode = ErrorResponse.getDefaultTitleMessageCode(ex.getClass());
|
||||||
|
this.typeMessageCode = ErrorResponse.getDefaultTypeMessageCode(ex.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -102,6 +105,12 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ErrorResponse.Builder typeMessageCode(String messageCode) {
|
||||||
|
this.typeMessageCode = messageCode;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ErrorResponse.Builder title(@Nullable String title) {
|
public ErrorResponse.Builder title(@Nullable String title) {
|
||||||
this.problemDetail.setTitle(title);
|
this.problemDetail.setTitle(title);
|
||||||
|
|
@ -131,7 +140,8 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder {
|
||||||
public ErrorResponse build() {
|
public ErrorResponse build() {
|
||||||
return new SimpleErrorResponse(
|
return new SimpleErrorResponse(
|
||||||
this.exception, this.statusCode, this.headers, this.problemDetail,
|
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 titleMessageCode;
|
||||||
|
|
||||||
|
private final String typeMessageCode;
|
||||||
|
|
||||||
SimpleErrorResponse(
|
SimpleErrorResponse(
|
||||||
Throwable ex, HttpStatusCode statusCode, @Nullable HttpHeaders headers, ProblemDetail problemDetail,
|
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.exception = ex;
|
||||||
this.statusCode = statusCode;
|
this.statusCode = statusCode;
|
||||||
|
|
@ -166,6 +179,7 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder {
|
||||||
this.detailMessageCode = detailMessageCode;
|
this.detailMessageCode = detailMessageCode;
|
||||||
this.detailMessageArguments = detailMessageArguments;
|
this.detailMessageArguments = detailMessageArguments;
|
||||||
this.titleMessageCode = titleMessageCode;
|
this.titleMessageCode = titleMessageCode;
|
||||||
|
this.typeMessageCode = typeMessageCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -198,6 +212,11 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder {
|
||||||
return this.titleMessageCode;
|
return this.titleMessageCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getTypeMessageCode() {
|
||||||
|
return this.typeMessageCode;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "ErrorResponse{status=" + this.statusCode + ", " +
|
return "ErrorResponse{status=" + this.statusCode + ", " +
|
||||||
|
|
|
||||||
|
|
@ -239,6 +239,17 @@ public interface ErrorResponse {
|
||||||
*/
|
*/
|
||||||
Builder type(URI type);
|
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.
|
* Set the underlying {@link ProblemDetail#setTitle(String) title} field.
|
||||||
* @return the same builder instance
|
* @return the same builder instance
|
||||||
|
|
@ -246,11 +257,9 @@ public interface ErrorResponse {
|
||||||
Builder title(@Nullable String title);
|
Builder title(@Nullable String title);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Customize the {@link MessageSource} code for looking up the value for
|
* Customize the {@link MessageSource} code to use to resolve the value
|
||||||
* the underlying {@link ProblemDetail#setTitle(String) title}.
|
* for {@link ProblemDetail#setTitle(String)}.
|
||||||
* <p>By default, set via
|
* <p>By default, set from {@link ErrorResponse#getDefaultTitleMessageCode(Class)}.
|
||||||
* {@link ErrorResponse#getDefaultTitleMessageCode(Class)} with the
|
|
||||||
* associated Exception type.
|
|
||||||
* @param messageCode the message code to use
|
* @param messageCode the message code to use
|
||||||
* @return the same builder instance
|
* @return the same builder instance
|
||||||
* @see ErrorResponse#getTitleMessageCode()
|
* @see ErrorResponse#getTitleMessageCode()
|
||||||
|
|
@ -270,11 +279,9 @@ public interface ErrorResponse {
|
||||||
Builder detail(String detail);
|
Builder detail(String detail);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Customize the {@link MessageSource} code for looking up the value for
|
* Customize the {@link MessageSource} code to use to resolve the value
|
||||||
* the underlying {@link #detail(String) detail}.
|
* for the {@link #detail(String)}.
|
||||||
* <p>By default, this is set to
|
* <p>By default, set from {@link ErrorResponse#getDefaultDetailMessageCode(Class, String)}.
|
||||||
* {@link ErrorResponse#getDefaultDetailMessageCode(Class, String)} with the
|
|
||||||
* associated Exception type.
|
|
||||||
* @param messageCode the message code to use
|
* @param messageCode the message code to use
|
||||||
* @return the same builder instance
|
* @return the same builder instance
|
||||||
* @see ErrorResponse#getDetailMessageCode()
|
* @see ErrorResponse#getDetailMessageCode()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue