Skip to content

Commit 48861b6

Browse files
committed
Add "title" message code to ErrorResponse.Builder
See gh-30566
1 parent 61eaa93 commit 48861b6

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

spring-web/src/main/java/org/springframework/web/DefaultErrorResponseBuilder.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder {
5050

5151
private String titleMessageCode;
5252

53+
private String typeMessageCode;
54+
5355

5456
DefaultErrorResponseBuilder(Throwable ex, HttpStatusCode statusCode, String detail) {
5557
Assert.notNull(ex, "Throwable is required");
@@ -60,6 +62,7 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder {
6062
this.problemDetail = ProblemDetail.forStatusAndDetail(statusCode, detail);
6163
this.detailMessageCode = ErrorResponse.getDefaultDetailMessageCode(ex.getClass(), null);
6264
this.titleMessageCode = ErrorResponse.getDefaultTitleMessageCode(ex.getClass());
65+
this.typeMessageCode = ErrorResponse.getDefaultTypeMessageCode(ex.getClass());
6366
}
6467

6568

@@ -102,6 +105,12 @@ public ErrorResponse.Builder type(URI type) {
102105
return this;
103106
}
104107

108+
@Override
109+
public ErrorResponse.Builder typeMessageCode(String messageCode) {
110+
this.typeMessageCode = messageCode;
111+
return this;
112+
}
113+
105114
@Override
106115
public ErrorResponse.Builder title(@Nullable String title) {
107116
this.problemDetail.setTitle(title);
@@ -131,7 +140,8 @@ public ErrorResponse.Builder property(String name, @Nullable Object value) {
131140
public ErrorResponse build() {
132141
return new SimpleErrorResponse(
133142
this.exception, this.statusCode, this.headers, this.problemDetail,
134-
this.detailMessageCode, this.detailMessageArguments, this.titleMessageCode);
143+
this.detailMessageCode, this.detailMessageArguments, this.titleMessageCode,
144+
this.typeMessageCode);
135145
}
136146

137147

@@ -155,9 +165,12 @@ private static class SimpleErrorResponse implements ErrorResponse {
155165

156166
private final String titleMessageCode;
157167

168+
private final String typeMessageCode;
169+
158170
SimpleErrorResponse(
159171
Throwable ex, HttpStatusCode statusCode, @Nullable HttpHeaders headers, ProblemDetail problemDetail,
160-
String detailMessageCode, @Nullable Object[] detailMessageArguments, String titleMessageCode) {
172+
String detailMessageCode, @Nullable Object[] detailMessageArguments, String titleMessageCode,
173+
String typeMessageCode) {
161174

162175
this.exception = ex;
163176
this.statusCode = statusCode;
@@ -166,6 +179,7 @@ private static class SimpleErrorResponse implements ErrorResponse {
166179
this.detailMessageCode = detailMessageCode;
167180
this.detailMessageArguments = detailMessageArguments;
168181
this.titleMessageCode = titleMessageCode;
182+
this.typeMessageCode = typeMessageCode;
169183
}
170184

171185
@Override
@@ -198,6 +212,11 @@ public String getTitleMessageCode() {
198212
return this.titleMessageCode;
199213
}
200214

215+
@Override
216+
public String getTypeMessageCode() {
217+
return this.typeMessageCode;
218+
}
219+
201220
@Override
202221
public String toString() {
203222
return "ErrorResponse{status=" + this.statusCode + ", " +

spring-web/src/main/java/org/springframework/web/ErrorResponse.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -239,18 +239,27 @@ interface Builder {
239239
*/
240240
Builder type(URI type);
241241

242+
/**
243+
* Customize the {@link MessageSource} code to use to resolve the value
244+
* for {@link ProblemDetail#setType(URI)}.
245+
* <p>By default, set from {@link ErrorResponse#getDefaultTypeMessageCode(Class)}.
246+
* @param messageCode the message code to use
247+
* @return the same builder instance
248+
* @since 6.1
249+
* @see ErrorResponse#getTypeMessageCode()
250+
*/
251+
Builder typeMessageCode(String messageCode);
252+
242253
/**
243254
* Set the underlying {@link ProblemDetail#setTitle(String) title} field.
244255
* @return the same builder instance
245256
*/
246257
Builder title(@Nullable String title);
247258

248259
/**
249-
* Customize the {@link MessageSource} code for looking up the value for
250-
* the underlying {@link ProblemDetail#setTitle(String) title}.
251-
* <p>By default, set via
252-
* {@link ErrorResponse#getDefaultTitleMessageCode(Class)} with the
253-
* associated Exception type.
260+
* Customize the {@link MessageSource} code to use to resolve the value
261+
* for {@link ProblemDetail#setTitle(String)}.
262+
* <p>By default, set from {@link ErrorResponse#getDefaultTitleMessageCode(Class)}.
254263
* @param messageCode the message code to use
255264
* @return the same builder instance
256265
* @see ErrorResponse#getTitleMessageCode()
@@ -270,11 +279,9 @@ interface Builder {
270279
Builder detail(String detail);
271280

272281
/**
273-
* Customize the {@link MessageSource} code for looking up the value for
274-
* the underlying {@link #detail(String) detail}.
275-
* <p>By default, this is set to
276-
* {@link ErrorResponse#getDefaultDetailMessageCode(Class, String)} with the
277-
* associated Exception type.
282+
* Customize the {@link MessageSource} code to use to resolve the value
283+
* for the {@link #detail(String)}.
284+
* <p>By default, set from {@link ErrorResponse#getDefaultDetailMessageCode(Class, String)}.
278285
* @param messageCode the message code to use
279286
* @return the same builder instance
280287
* @see ErrorResponse#getDetailMessageCode()

0 commit comments

Comments
 (0)