|
30 | 30 | import java.util.HashMap;
|
31 | 31 | import java.util.List;
|
32 | 32 | import java.util.Map;
|
| 33 | +import java.util.regex.Matcher; |
| 34 | +import java.util.regex.Pattern; |
33 | 35 |
|
34 | 36 | import static io.github.talelin.autoconfigure.util.RequestUtil.getSimpleRequest;
|
35 | 37 |
|
@@ -196,10 +198,16 @@ public UnifyResponseVO processException(HttpMessageNotReadableException exceptio
|
196 | 198 | UnifyResponseVO result = new UnifyResponseVO();
|
197 | 199 | result.setRequest(getSimpleRequest(request));
|
198 | 200 | String errorMessage = CodeMessageConfiguration.getMessage(10170);
|
199 |
| - if (!StringUtils.hasText(errorMessage)) { |
200 |
| - result.setMessage(exception.getMessage()); |
| 201 | + Throwable cause = exception.getCause(); |
| 202 | + if(cause != null) { |
| 203 | + String msg = this.convertMessage(cause); |
| 204 | + result.setMessage(msg); |
201 | 205 | } else {
|
202 |
| - result.setMessage(errorMessage); |
| 206 | + if (!StringUtils.hasText(errorMessage)) { |
| 207 | + result.setMessage(exception.getMessage()); |
| 208 | + } else { |
| 209 | + result.setMessage(errorMessage); |
| 210 | + } |
203 | 211 | }
|
204 | 212 | result.setCode(Code.PARAMETER_ERROR.getCode());
|
205 | 213 | response.setStatus(HttpStatus.BAD_REQUEST.value());
|
@@ -252,4 +260,26 @@ public UnifyResponseVO processException(Exception exception, HttpServletRequest
|
252 | 260 | response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
|
253 | 261 | return result;
|
254 | 262 | }
|
| 263 | + |
| 264 | + /** |
| 265 | + * 传参类型错误时,用于消息转换 |
| 266 | + * @param throwable |
| 267 | + * @return 错误信息 |
| 268 | + */ |
| 269 | + private String convertMessage(Throwable throwable) { |
| 270 | + String error = throwable.toString(); |
| 271 | + String regulation = "\\[\"(.*?)\"]+"; |
| 272 | + Pattern pattern = Pattern.compile(regulation); |
| 273 | + Matcher matcher = pattern.matcher(error); |
| 274 | + String group = ""; |
| 275 | + if(matcher.find()) { |
| 276 | + String matchString = matcher.group(); |
| 277 | + matchString = matchString |
| 278 | + .replace("[","") |
| 279 | + .replace("]",""); |
| 280 | + matchString = matchString.replaceAll("\\\"","") + "字段类型错误"; |
| 281 | + group += matchString; |
| 282 | + } |
| 283 | + return group; |
| 284 | + } |
255 | 285 | }
|
0 commit comments