2023-02-16 15:28:15 +08:00
|
|
|
|
package com.zhgd.exception;
|
|
|
|
|
|
|
2023-05-10 14:08:46 +08:00
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
2023-02-16 15:28:15 +08:00
|
|
|
|
import com.zhgd.jeecg.common.api.vo.Result;
|
2023-05-10 14:08:46 +08:00
|
|
|
|
import com.zhgd.jeecg.common.constant.CommonConstant;
|
2023-02-16 15:28:15 +08:00
|
|
|
|
import com.zhgd.jeecg.common.execption.OpenAlertException;
|
2024-04-07 18:23:25 +08:00
|
|
|
|
import com.zhgd.jeecg.common.execption.OpenPromptException;
|
2023-02-16 15:28:15 +08:00
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2023-05-10 14:08:46 +08:00
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
|
import org.springframework.validation.BindingResult;
|
|
|
|
|
|
import org.springframework.validation.FieldError;
|
|
|
|
|
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
2023-02-16 15:28:15 +08:00
|
|
|
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
2023-05-10 14:08:46 +08:00
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
import java.util.stream.Collectors;
|
2023-02-16 15:28:15 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @program: itbgpproject
|
|
|
|
|
|
* @description: 统一异常处理
|
|
|
|
|
|
* @author: Mr.Peng
|
|
|
|
|
|
* @create: 2020-08-05 14:53
|
|
|
|
|
|
**/
|
|
|
|
|
|
@ResponseBody
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
|
@ControllerAdvice
|
|
|
|
|
|
public class ExceptionHandlerAdvice {
|
|
|
|
|
|
|
|
|
|
|
|
@ExceptionHandler(Exception.class)
|
|
|
|
|
|
@ResponseBody
|
|
|
|
|
|
public Result<Object> restError(HttpServletRequest request, HttpServletResponse response, Exception ex) {
|
|
|
|
|
|
Result<Object> result = new Result<>();
|
|
|
|
|
|
if (ex instanceof OpenAlertException) {
|
|
|
|
|
|
OpenAlertException e = (OpenAlertException) ex;
|
|
|
|
|
|
result.setCode(e.getCode());
|
|
|
|
|
|
result.setMessage(e.getMessage());
|
|
|
|
|
|
result.setSuccess(false);
|
|
|
|
|
|
} else if (ex instanceof CustomException) {
|
|
|
|
|
|
CustomException appException = (CustomException) ex;
|
|
|
|
|
|
result.setCode(403);
|
|
|
|
|
|
result.setMessage(appException.getMessage());
|
|
|
|
|
|
result.setSuccess(false);
|
2024-04-07 18:23:25 +08:00
|
|
|
|
} else if (ex instanceof OpenPromptException) {
|
|
|
|
|
|
OpenPromptException appException = (OpenPromptException) ex;
|
2023-08-17 18:10:07 +08:00
|
|
|
|
result.setCode(200);
|
|
|
|
|
|
result.setMessage(appException.getMessage());
|
|
|
|
|
|
result.setResult(appException.getResult());
|
2023-02-16 15:28:15 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
result.error500(ex.getMessage() == null ? "操作中出现空指针!" : ex.getMessage());
|
|
|
|
|
|
ex.printStackTrace();
|
|
|
|
|
|
}
|
2023-04-28 09:34:13 +08:00
|
|
|
|
log.error("异常请求路径:" + request.getRequestURI() + "--" + result.getMessage());
|
|
|
|
|
|
log.error("errr:", ex);
|
2023-02-16 15:28:15 +08:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
2023-05-10 14:08:46 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Bean校验友好返回信息
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param e
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
@ExceptionHandler(value = MethodArgumentNotValidException.class)
|
|
|
|
|
|
@ResponseBody
|
|
|
|
|
|
public Result handleVaildException(MethodArgumentNotValidException e) {
|
|
|
|
|
|
log.error("数据校验出现问题{},异常类型:{}", e.getMessage(), e.getClass());
|
|
|
|
|
|
BindingResult bindingResult = e.getBindingResult();
|
|
|
|
|
|
|
|
|
|
|
|
Map<String, String> errorMap = new HashMap<>();
|
|
|
|
|
|
bindingResult.getFieldErrors().forEach((fieldError) -> {
|
|
|
|
|
|
errorMap.put(fieldError.getField(), fieldError.getDefaultMessage());
|
|
|
|
|
|
});
|
|
|
|
|
|
List<String> messageList = bindingResult.getFieldErrors().stream().map(FieldError::getDefaultMessage).collect(Collectors.toList());
|
|
|
|
|
|
Result<Object> r = new Result<>();
|
|
|
|
|
|
r.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500);
|
|
|
|
|
|
r.setMessage(StrUtil.format("请输入合理的数据:{}", StringUtils.join(messageList)));
|
|
|
|
|
|
r.setResult(errorMap);
|
|
|
|
|
|
return r;
|
|
|
|
|
|
}
|
2023-02-16 15:28:15 +08:00
|
|
|
|
}
|