提交 0ff28aa5 作者: zhenjie

异常处理

上级 377bc317
package com.mmc.csf.release.error;
import com.mmc.csf.common.util.web.BizException;
import com.mmc.csf.common.util.web.ResultBody;
import com.mmc.csf.common.util.web.ResultEnum;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author 作者 geDuo
* @version 创建时间:2021年8月27日 上午10:15:30
* @explain 类说明
*/
@ControllerAdvice
public class GlobalExceptionHandler {
/**
* 统一处理参数校验错误异常(非Spring接口数据绑定验证)
*
* @param e
* @return
*/
@ExceptionHandler(BindException.class)
@ResponseBody
public ResultBody processValidException(BindException e) {
// 获取校验错误结果信息,并将信息组装
List<String> errorStringList = e.getBindingResult().getAllErrors().stream().map(ObjectError::getDefaultMessage)
.collect(Collectors.toList());
String errorMessage = String.join("; ", errorStringList);
return ResultBody.error(ResultEnum.BODY_NOT_MATCH.getResultCode(), errorMessage);
}
/**
* 统一处理参数校验错误异常
*
* @param e
* @return
*/
@ExceptionHandler(IllegalArgumentException.class)
@ResponseBody
public ResultBody processValidException(IllegalArgumentException e) {
String errorMessage = String.join("; ", e.getMessage());
return ResultBody.error(ResultEnum.BODY_NOT_MATCH.getResultCode(), errorMessage);
}
@ExceptionHandler(ConstraintViolationException.class)
@ResponseBody
public ResultBody processValidException(ConstraintViolationException e) {
String errorMessage = e.getConstraintViolations().stream().map(ConstraintViolation::getMessage)
.collect(Collectors.joining(","));
return ResultBody.error(ResultEnum.BODY_NOT_MATCH.getResultCode(), errorMessage);
}
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public ResultBody processValidException(MethodArgumentNotValidException e) {
String errorMessage = e.getBindingResult().getAllErrors().stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining(","));
return ResultBody.error(ResultEnum.BODY_NOT_MATCH.getResultCode(), errorMessage);
}
@ExceptionHandler(MissingServletRequestParameterException.class)
@ResponseBody
public ResultBody processValidException(MissingServletRequestParameterException e) {
String errorMessage = e.getParameterName() + " 不能为空";
return ResultBody.error(ResultEnum.BODY_NOT_MATCH.getResultCode(), errorMessage);
}
@ExceptionHandler(MissingPathVariableException.class)
@ResponseBody
public ResultBody processValidException(MissingPathVariableException e) {
String errorMessage = e.getVariableName() + " 不能为空";
return ResultBody.error(ResultEnum.BODY_NOT_MATCH.getResultCode(), errorMessage);
}
@ExceptionHandler(BizException.class)
@ResponseBody
public ResultBody processValidException(BizException e) {
return ResultBody.error(e.getErrorCode(), e.getErrorMsg());
}
}
......@@ -110,6 +110,16 @@ iuav:
pmsapp:
url: http://127.0.0.1:8099
wx:
sub:
appid: wx5c6a105a0ddca4c5
secret: 96c75255dd26f82f8d55e15b59e101c7
app:
id: wx18b7883acd204278
secret: 28afe74ba373830237a8133a7431ee82
miniprogram-state: trial
env-version: trial
##feign
#feign:
# client:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论