|
16 | 16 |
|
17 | 17 | package org.springframework.web;
|
18 | 18 |
|
| 19 | +import java.net.URI; |
19 | 20 | import java.util.Locale;
|
| 21 | +import java.util.function.Consumer; |
20 | 22 |
|
21 | 23 | import org.springframework.context.MessageSource;
|
22 | 24 | import org.springframework.http.HttpHeaders;
|
@@ -148,35 +150,124 @@ static String getDefaultTitleMessageCode(Class<?> exceptionType) {
|
148 | 150 | return "problemDetail.title." + exceptionType.getName();
|
149 | 151 | }
|
150 | 152 |
|
| 153 | + |
151 | 154 | /**
|
152 |
| - * Map the given Exception to an {@link ErrorResponse}. |
153 |
| - * @param ex the Exception, mostly to derive message codes, if not provided |
154 |
| - * @param status the response status to use |
155 |
| - * @param headers optional headers to add to the response |
156 |
| - * @param defaultDetail default value for the "detail" field |
157 |
| - * @param detailMessageCode the code to use to look up the "detail" field |
158 |
| - * through a {@code MessageSource}, falling back on |
159 |
| - * {@link #getDefaultDetailMessageCode(Class, String)} |
160 |
| - * @param detailMessageArguments the arguments to go with the detailMessageCode |
161 |
| - * @return the created {@code ErrorResponse} instance |
| 155 | + * Static factory method to build an instance via |
| 156 | + * {@link #builder(Throwable, HttpStatusCode, String)}. |
162 | 157 | */
|
163 |
| - static ErrorResponse createFor( |
164 |
| - Exception ex, HttpStatusCode status, @Nullable HttpHeaders headers, |
165 |
| - String defaultDetail, @Nullable String detailMessageCode, @Nullable Object[] detailMessageArguments) { |
| 158 | + static ErrorResponse create(Throwable ex, HttpStatusCode statusCode, String detail) { |
| 159 | + return builder(ex, statusCode, detail).build(); |
| 160 | + } |
166 | 161 |
|
167 |
| - if (detailMessageCode == null) { |
168 |
| - detailMessageCode = ErrorResponse.getDefaultDetailMessageCode(ex.getClass(), null); |
169 |
| - } |
| 162 | + /** |
| 163 | + * Return a builder to create an {@code ErrorResponse} instance. |
| 164 | + * @param ex the underlying exception that lead to the error response; |
| 165 | + * mainly to derive default values for the |
| 166 | + * {@link #getDetailMessageCode() detail message code} and for the |
| 167 | + * {@link #getTitleMessageCode() title message code}. |
| 168 | + * @param statusCode the status code to set the response to |
| 169 | + * @param detail the default value for the |
| 170 | + * {@link ProblemDetail#setDetail(String) detail} field, unless overridden |
| 171 | + * by a {@link MessageSource} lookup with {@link #getDetailMessageCode()} |
| 172 | + */ |
| 173 | + static Builder builder(Throwable ex, HttpStatusCode statusCode, String detail) { |
| 174 | + return new DefaultErrorResponseBuilder(ex, statusCode, detail); |
| 175 | + } |
170 | 176 |
|
171 |
| - ErrorResponseException errorResponse = new ErrorResponseException( |
172 |
| - status, ProblemDetail.forStatusAndDetail(status, defaultDetail), null, |
173 |
| - detailMessageCode, detailMessageArguments); |
174 | 177 |
|
175 |
| - if (headers != null) { |
176 |
| - errorResponse.getHeaders().putAll(headers); |
177 |
| - } |
| 178 | + /** |
| 179 | + * Builder for an {@code ErrorResponse}. |
| 180 | + */ |
| 181 | + interface Builder { |
| 182 | + |
| 183 | + /** |
| 184 | + * Add the given header value(s) under the given name. |
| 185 | + * @param headerName the header name |
| 186 | + * @param headerValues the header value(s) |
| 187 | + * @return the same builder instance |
| 188 | + * @see HttpHeaders#add(String, String) |
| 189 | + */ |
| 190 | + Builder header(String headerName, String... headerValues); |
| 191 | + |
| 192 | + /** |
| 193 | + * Manipulate this response's headers with the given consumer. This is |
| 194 | + * useful to {@linkplain HttpHeaders#set(String, String) overwrite} or |
| 195 | + * {@linkplain HttpHeaders#remove(Object) remove} existing values, or |
| 196 | + * use any other {@link HttpHeaders} methods. |
| 197 | + * @param headersConsumer a function that consumes the {@code HttpHeaders} |
| 198 | + * @return the same builder instance |
| 199 | + */ |
| 200 | + Builder headers(Consumer<HttpHeaders> headersConsumer); |
| 201 | + |
| 202 | + /** |
| 203 | + * Set the underlying {@link ProblemDetail#setDetail(String)}. |
| 204 | + * @return the same builder instance |
| 205 | + */ |
| 206 | + Builder detail(String detail); |
| 207 | + |
| 208 | + /** |
| 209 | + * Customize the {@link MessageSource} code for looking up the value for |
| 210 | + * the underlying {@link #detail(String)}. |
| 211 | + * <p>By default, this is set to |
| 212 | + * {@link ErrorResponse#getDefaultDetailMessageCode(Class, String)} with the |
| 213 | + * associated Exception type. |
| 214 | + * @param messageCode the message code to use |
| 215 | + * @return the same builder instance |
| 216 | + * @see ErrorResponse#getDetailMessageCode() |
| 217 | + */ |
| 218 | + Builder detailMessageCode(String messageCode); |
| 219 | + |
| 220 | + /** |
| 221 | + * Set the arguments to provide to the {@link MessageSource} lookup for |
| 222 | + * {@link #detailMessageCode(String)}. |
| 223 | + * @param messageArguments the arguments to provide |
| 224 | + * @return the same builder instance |
| 225 | + * @see ErrorResponse#getDetailMessageArguments() |
| 226 | + */ |
| 227 | + Builder detailMessageArguments(Object... messageArguments); |
| 228 | + |
| 229 | + /** |
| 230 | + * Set the underlying {@link ProblemDetail#setTitle(String)} field. |
| 231 | + * @return the same builder instance |
| 232 | + */ |
| 233 | + Builder type(URI type); |
| 234 | + |
| 235 | + /** |
| 236 | + * Set the underlying {@link ProblemDetail#setTitle(String)} field. |
| 237 | + * @return the same builder instance |
| 238 | + */ |
| 239 | + Builder title(@Nullable String title); |
| 240 | + |
| 241 | + /** |
| 242 | + * Customize the {@link MessageSource} code for looking up the value for |
| 243 | + * the underlying {@link ProblemDetail#setTitle(String)}. |
| 244 | + * <p>By default, set via |
| 245 | + * {@link ErrorResponse#getDefaultTitleMessageCode(Class)} with the |
| 246 | + * associated Exception type. |
| 247 | + * @param messageCode the message code to use |
| 248 | + * @return the same builder instance |
| 249 | + * @see ErrorResponse#getTitleMessageCode() |
| 250 | + */ |
| 251 | + Builder titleMessageCode(String messageCode); |
| 252 | + |
| 253 | + /** |
| 254 | + * Set the underlying {@link ProblemDetail#setInstance(URI)} field. |
| 255 | + * @return the same builder instance |
| 256 | + */ |
| 257 | + Builder instance(@Nullable URI instance); |
| 258 | + |
| 259 | + /** |
| 260 | + * Set a "dynamic" {@link ProblemDetail#setProperty(String, Object) |
| 261 | + * property} on the underlying {@code ProblemDetail}. |
| 262 | + * @return the same builder instance |
| 263 | + */ |
| 264 | + Builder property(String name, Object value); |
| 265 | + |
| 266 | + /** |
| 267 | + * Build the {@code ErrorResponse} instance. |
| 268 | + */ |
| 269 | + ErrorResponse build(); |
178 | 270 |
|
179 |
| - return errorResponse; |
180 | 271 | }
|
181 | 272 |
|
182 | 273 | }
|
0 commit comments