Skip to content

Commit

Permalink
!27 fix(RestExceptionTranslator): 修复FieldError为空问题
Browse files Browse the repository at this point in the history
* fix(RestExceptionTranslator): 修复FieldError为空问题
  • Loading branch information
mymx2 authored and ChunMengLu committed Mar 6, 2024
1 parent 0f2644a commit 1aa79ce
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
Expand Down Expand Up @@ -158,7 +159,15 @@ public R<Object> handleError(HttpMediaTypeNotAcceptableException e) {
*/
private static R<Object> handleError(BindingResult result) {
FieldError error = result.getFieldError();
String message = String.format("%s:%s", error.getField(), error.getDefaultMessage());
String message = "";
if (error != null) {
message = String.format("%s:%s", error.getField(), error.getDefaultMessage());
} else {
ObjectError globalError = result.getGlobalError();
if (globalError != null) {
message = globalError.getDefaultMessage();
}
}
return R.fail(SystemCode.PARAM_BIND_ERROR, message);
}

Expand Down

0 comments on commit 1aa79ce

Please sign in to comment.