Skip to content

Commit 98776c5

Browse files
towelongsunlin92
authored andcommitted
fix:#184
1 parent 7db150c commit 98776c5

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

src/main/java/io/github/talelin/latticy/common/exception/RestExceptionHandler.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import java.util.HashMap;
3131
import java.util.List;
3232
import java.util.Map;
33+
import java.util.regex.Matcher;
34+
import java.util.regex.Pattern;
3335

3436
import static io.github.talelin.autoconfigure.util.RequestUtil.getSimpleRequest;
3537

@@ -194,10 +196,16 @@ public UnifyResponseVO processException(HttpMessageNotReadableException exceptio
194196
UnifyResponseVO result = new UnifyResponseVO();
195197
result.setRequest(getSimpleRequest(request));
196198
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);
199203
} else {
200-
result.setMessage(errorMessage);
204+
if (StrUtil.isBlank(errorMessage)) {
205+
result.setMessage(exception.getMessage());
206+
} else {
207+
result.setMessage(errorMessage);
208+
}
201209
}
202210
result.setCode(Code.PARAMETER_ERROR.getCode());
203211
response.setStatus(HttpStatus.BAD_REQUEST.value());
@@ -250,4 +258,26 @@ public UnifyResponseVO processException(Exception exception, HttpServletRequest
250258
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
251259
return result;
252260
}
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+
}
253283
}

0 commit comments

Comments
 (0)