Commit 1fa99af1 by guojuxing

全局异常类捕获还原代码

parent fe52e528
......@@ -29,6 +29,32 @@ public class GlobalExceptionHandler {
private static Logger logger = LogManager.getLogger(GlobalExceptionHandler.class);
/**
* 参数校验异常统一处理
* @param e
* @return
*/
@ExceptionHandler(BindException.class)
public RestResponse customException(BindException e) {
List<FieldError> fieldErrors = e.getFieldErrors();
StringBuilder errorMessage = new StringBuilder();
fieldErrors.forEach(fieldError -> {
errorMessage
.append(fieldError.getDefaultMessage())
.append(",");
});
String error = errorMessage.toString();
return getRestResponse(ErrorCode.PARAMETER_ERROR.getCode(), error.substring(0, error.length() - 1));
}
@ExceptionHandler(ConstraintViolationException.class)
public RestResponse constraintViolationException(ConstraintViolationException e) {
Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
String paramName = constraintViolations.iterator().next().getPropertyPath().toString();
String paramError = constraintViolations.iterator().next().getMessage();
return getRestResponse(ErrorCode.PARAMETER_ERROR.getCode(), getFailFastMsg(paramName, paramError));
}
@ExceptionHandler(RuntimeException.class)
public RestResponse runtimeException(RuntimeException e){
logger.warn("异常:{}", e);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment