|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2024 the original author or authors. |
| 2 | + * Copyright 2012-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
20 | 20 | import java.io.StringWriter;
|
21 | 21 | import java.util.Date;
|
22 | 22 | import java.util.LinkedHashMap;
|
23 |
| -import java.util.List; |
24 | 23 | import java.util.Map;
|
25 | 24 |
|
26 | 25 | import jakarta.servlet.RequestDispatcher;
|
27 | 26 | import jakarta.servlet.ServletException;
|
28 | 27 | import jakarta.servlet.http.HttpServletRequest;
|
29 | 28 | import jakarta.servlet.http.HttpServletResponse;
|
30 | 29 |
|
| 30 | +import org.springframework.boot.web.error.Error; |
31 | 31 | import org.springframework.boot.web.error.ErrorAttributeOptions;
|
32 | 32 | import org.springframework.boot.web.error.ErrorAttributeOptions.Include;
|
33 |
| -import org.springframework.boot.web.error.ErrorWrapper; |
34 | 33 | import org.springframework.core.Ordered;
|
35 | 34 | import org.springframework.core.annotation.Order;
|
36 | 35 | import org.springframework.http.HttpStatus;
|
37 | 36 | import org.springframework.util.ObjectUtils;
|
38 | 37 | import org.springframework.util.StringUtils;
|
39 | 38 | import org.springframework.validation.BindingResult;
|
40 |
| -import org.springframework.validation.ObjectError; |
41 | 39 | import org.springframework.validation.method.MethodValidationResult;
|
42 | 40 | import org.springframework.web.context.request.RequestAttributes;
|
43 | 41 | import org.springframework.web.context.request.WebRequest;
|
|
53 | 51 | * <li>error - The error reason</li>
|
54 | 52 | * <li>exception - The class name of the root exception (if configured)</li>
|
55 | 53 | * <li>message - The exception message (if configured)</li>
|
56 |
| - * <li>errors - Any validation errors wrapped in {@link ErrorWrapper}, derived from a |
| 54 | + * <li>errors - Any validation errors wrapped in {@link Error}, derived from a |
57 | 55 | * {@link BindingResult} or {@link MethodValidationResult} exception (if configured)</li>
|
58 | 56 | * <li>trace - The exception stack trace (if configured)</li>
|
59 | 57 | * <li>path - The URL path when the exception was raised</li>
|
@@ -143,27 +141,27 @@ private void addErrorMessage(Map<String, Object> errorAttributes, WebRequest web
|
143 | 141 | BindingResult bindingResult = extractBindingResult(error);
|
144 | 142 | if (bindingResult != null) {
|
145 | 143 | addMessageAndErrorsFromBindingResult(errorAttributes, bindingResult);
|
| 144 | + return; |
146 | 145 | }
|
147 |
| - else { |
148 |
| - MethodValidationResult methodValidationResult = extractMethodValidationResult(error); |
149 |
| - if (methodValidationResult != null) { |
150 |
| - addMessageAndErrorsFromMethodValidationResult(errorAttributes, methodValidationResult); |
151 |
| - } |
152 |
| - else { |
153 |
| - addExceptionErrorMessage(errorAttributes, webRequest, error); |
154 |
| - } |
| 146 | + MethodValidationResult methodValidationResult = extractMethodValidationResult(error); |
| 147 | + if (methodValidationResult != null) { |
| 148 | + addMessageAndErrorsFromMethodValidationResult(errorAttributes, methodValidationResult); |
| 149 | + return; |
155 | 150 | }
|
| 151 | + addExceptionErrorMessage(errorAttributes, webRequest, error); |
| 152 | + } |
| 153 | + |
| 154 | + private void addMessageAndErrorsFromBindingResult(Map<String, Object> errorAttributes, BindingResult result) { |
| 155 | + errorAttributes.put("message", "Validation failed for object='%s'. Error count: %s" |
| 156 | + .formatted(result.getObjectName(), result.getAllErrors().size())); |
| 157 | + errorAttributes.put("errors", Error.wrap(result.getAllErrors())); |
156 | 158 | }
|
157 | 159 |
|
158 | 160 | private void addMessageAndErrorsFromMethodValidationResult(Map<String, Object> errorAttributes,
|
159 | 161 | MethodValidationResult result) {
|
160 |
| - List<ErrorWrapper> errors = result.getAllErrors() |
161 |
| - .stream() |
162 |
| - .map(ErrorWrapper::new) |
163 |
| - .toList(); |
164 |
| - errorAttributes.put("message", |
165 |
| - "Validation failed for method='" + result.getMethod() + "'. Error count: " + errors.size()); |
166 |
| - errorAttributes.put("errors", errors); |
| 162 | + errorAttributes.put("message", "Validation failed for method='%s'. Error count: %s" |
| 163 | + .formatted(result.getMethod(), result.getAllErrors().size())); |
| 164 | + errorAttributes.put("errors", Error.wrap(result.getAllErrors())); |
167 | 165 | }
|
168 | 166 |
|
169 | 167 | private void addExceptionErrorMessage(Map<String, Object> errorAttributes, WebRequest webRequest, Throwable error) {
|
@@ -195,17 +193,6 @@ protected String getMessage(WebRequest webRequest, Throwable error) {
|
195 | 193 | return "No message available";
|
196 | 194 | }
|
197 | 195 |
|
198 |
| - private void addMessageAndErrorsFromBindingResult(Map<String, Object> errorAttributes, BindingResult result) { |
199 |
| - addMessageAndErrorsForValidationFailure(errorAttributes, "object='" + result.getObjectName() + "'", |
200 |
| - result.getAllErrors()); |
201 |
| - } |
202 |
| - |
203 |
| - private void addMessageAndErrorsForValidationFailure(Map<String, Object> errorAttributes, String validated, |
204 |
| - List<ObjectError> errors) { |
205 |
| - errorAttributes.put("message", "Validation failed for " + validated + ". Error count: " + errors.size()); |
206 |
| - errorAttributes.put("errors", errors); |
207 |
| - } |
208 |
| - |
209 | 196 | private BindingResult extractBindingResult(Throwable error) {
|
210 | 197 | if (error instanceof BindingResult bindingResult) {
|
211 | 198 | return bindingResult;
|
|
0 commit comments