|
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 |
|
@@ -194,10 +196,16 @@ public UnifyResponseVO processException(HttpMessageNotReadableException exceptio
|
194 | 196 | UnifyResponseVO result = new UnifyResponseVO();
|
195 | 197 | result.setRequest(getSimpleRequest(request));
|
196 | 198 | String errorMessage = CodeMessageConfiguration.getMessage(10170);
|
197 |
| - if (StrUtil.isBlank(errorMessage)) { |
198 |
| - result.setMessage(exception.getMessage()); |
| 199 | + Throwable cause = exception.getCause(); |
| 200 | + if(cause != null) { |
| 201 | + String msg = this.convertMessage(cause); |
| 202 | + result.setMessage(msg); |
199 | 203 | } else {
|
200 |
| - result.setMessage(errorMessage); |
| 204 | + if (StrUtil.isBlank(errorMessage)) { |
| 205 | + result.setMessage(exception.getMessage()); |
| 206 | + } else { |
| 207 | + result.setMessage(errorMessage); |
| 208 | + } |
201 | 209 | }
|
202 | 210 | result.setCode(Code.PARAMETER_ERROR.getCode());
|
203 | 211 | response.setStatus(HttpStatus.BAD_REQUEST.value());
|
@@ -250,4 +258,26 @@ public UnifyResponseVO processException(Exception exception, HttpServletRequest
|
250 | 258 | response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
|
251 | 259 | return result;
|
252 | 260 | }
|
| 261 | + |
| 262 | + /** |
| 263 | + * 传参类型错误时,用于消息转换 |
| 264 | + * @param throwable |
| 265 | + * @return 错误信息 |
| 266 | + */ |
| 267 | + private String convertMessage(Throwable throwable) { |
| 268 | + String error = throwable.toString(); |
| 269 | + String regulation = "\\[\"(.*?)\"]+"; |
| 270 | + Pattern pattern = Pattern.compile(regulation); |
| 271 | + Matcher matcher = pattern.matcher(error); |
| 272 | + String group = ""; |
| 273 | + if(matcher.find()) { |
| 274 | + String matchString = matcher.group(); |
| 275 | + matchString = matchString |
| 276 | + .replace("[","") |
| 277 | + .replace("]",""); |
| 278 | + matchString = matchString.replaceAll("\\\"","") + "字段类型错误"; |
| 279 | + group += matchString; |
| 280 | + } |
| 281 | + return group; |
| 282 | + } |
253 | 283 | }
|
0 commit comments