Allow custom ProblemDetail in ErrorResponse.Builder

Closes gh-30568
This commit is contained in:
rstoyanchev 2023-06-14 16:45:14 +01:00
parent 2e43412a82
commit dcba9475ba
2 changed files with 13 additions and 6 deletions

View File

@ -53,13 +53,11 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder {
private Object[] detailMessageArguments;
DefaultErrorResponseBuilder(Throwable ex, HttpStatusCode statusCode, String detail) {
DefaultErrorResponseBuilder(Throwable ex, ProblemDetail problemDetail) {
Assert.notNull(ex, "Throwable is required");
Assert.notNull(statusCode, "HttpStatusCode is required");
Assert.notNull(detail, "`detail` is required");
this.exception = ex;
this.statusCode = statusCode;
this.problemDetail = ProblemDetail.forStatusAndDetail(statusCode, detail);
this.statusCode = HttpStatusCode.valueOf(problemDetail.getStatus());
this.problemDetail = problemDetail;
this.typeMessageCode = ErrorResponse.getDefaultTypeMessageCode(ex.getClass());
this.titleMessageCode = ErrorResponse.getDefaultTitleMessageCode(ex.getClass());
this.detailMessageCode = ErrorResponse.getDefaultDetailMessageCode(ex.getClass(), null);

View File

@ -205,7 +205,16 @@ public interface ErrorResponse {
* by a {@link MessageSource} lookup with {@link #getDetailMessageCode()}
*/
static Builder builder(Throwable ex, HttpStatusCode statusCode, String detail) {
return new DefaultErrorResponseBuilder(ex, statusCode, detail);
return builder(ex, ProblemDetail.forStatusAndDetail(statusCode, detail));
}
/**
* Variant of {@link #builder(Throwable, HttpStatusCode, String)} for use
* with a custom {@link ProblemDetail} instance.
* @since 6.1
*/
static Builder builder(Throwable ex, ProblemDetail problemDetail) {
return new DefaultErrorResponseBuilder(ex, problemDetail);
}