问题描述
提示:这里描述项目中遇到的问题:
自定义filter抛出的异常不走自定义全局异常
原因分析:
自定义filter过滤器时抛出的异常走不到controller去因此就不会走自定义的全局异常
{
"timestamp": "2023-09-06 16:29:30",
"status": 500,
"error": "Internal Server Error",
"path": "/openapi/openApi/test"
}
解决方案:
1、自定义一个捕捉过滤器异常的filter
public class ExceptionFilter implements Filter {
public static final String ERROR_CONTROLLER_PATH = "/error/throw";
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
try {
filterChain.doFilter(request, response);
} catch (BizException e) {
e.printStackTrace();
// 传递异常信息
request.setAttribute("filterError", e);
// 指定处理该请求的处理器
request.getRequestDispatcher(ERROR_CONTROLLER_PATH).forward(request, response);
}
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
}
注册过滤器:
@Configuration
public class OpenApiFilterConfig {
// Filter代码类
@Bean
public ExceptionFilter ExceptionFilter() {
return new ExceptionFilter();
}
@Bean
public FilterRegistrationBean exceptionFilter(ExceptionFilter exceptionFilter) {
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(exceptionFilter);
registrationBean.setName("exceptionFilter");
registrationBean.setOrder(-1);
return registrationBean;
}
}
定义异常处理controller:
@RestController
public class ExceptionController {
public static final String ERROR_CONTROLLER_PATH = "/error/throw";
@RequestMapping(ERROR_CONTROLLER_PATH)
public void handleException(HttpServletRequest request) {
throw (BizException) request.getAttribute("filterError");
}
}
自定义全局异常
@ControllerAdvice
public class GlobalExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
/**
* 处理自定义的业务异常
*
* @param req
* @param e
* @return
*/
@ExceptionHandler(value = BizException.class)
@ResponseBody
public ResultBody bizExceptionHandler(HttpServletRequest req, BizException e) {
logger.error("发生业务异常!原因是:{}", e.getErrorMsg());
return ResultBody.error(e.getErrorCode(), e.getErrorMsg(),e.getMessage());
}
/**
* 处理空指针的异常
*
* @param req
* @param e
* @return
*/
@ExceptionHandler(value = NullPointerException.class)
@ResponseBody
public ResultBody exceptionHandler(HttpServletRequest req, NullPointerException e) {
logger.error("发生空指针异常!原因是:", e);
return ResultBody.error(CommonEnum.BODY_NOT_MATCH);
}
/**
* 处理其他异常
*
* @param req
* @param e
* @return
*/
@ExceptionHandler(value = Exception.class)
@ResponseBody
public ResultBody exceptionHandler(HttpServletRequest req, Exception e) {
logger.error("未知异常!原因是:", e);
return ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
}
相关封装类和接口
public class BizException extends RuntimeException {
private static final long serialVersionUID = 1L;
/**
* 错误码
*/
protected String errorCode;
/**
* 错误信息
*/
protected String errorMsg;
/**
* 错误信息码
*/
protected String message;
public BizException() {
super();
}
public BizException(BaseErrorInfoInterface errorInfoInterface) {
super(errorInfoInterface.getResultCode());
this.errorCode = errorInfoInterface.getResultCode();
this.errorMsg = errorInfoInterface.getResultMsg();
this.message = errorInfoInterface.getMessage();
}
public BizException(BaseErrorInfoInterface errorInfoInterface, Throwable cause) {
super(errorInfoInterface.getResultCode(), cause);
this.errorCode = errorInfoInterface.getResultCode();
this.errorMsg = errorInfoInterface.getResultMsg();
this.message = errorInfoInterface.getMessage();
}
public BizException(String errorMsg) {
super(errorMsg);
this.message = errorMsg;
}
public BizException(String errorCode, String errorMsg) {
super(errorCode);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public BizException(String errorCode, String errorMsg, String message) {
super(errorCode);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
this.message = message;
}
public BizException(String errorCode, String errorMsg, Throwable cause) {
super(errorCode, cause);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public BizException(String errorCode, String errorMsg, String message, Throwable cause) {
super(errorCode, cause);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
this.message = message;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public Throwable fillInStackTrace() {
return this;
}
}
public interface BaseErrorInfoInterface {
/**
* 错误码
*/
String getResultCode();
/**
* 错误描述
*/
String getResultMsg();
/**
* 错误信息码
*/
String getMessage();
}
public enum CommonEnum implements BaseErrorInfoInterface {
// 数据操作错误定义
SUCCESS("200", "成功!", "SUCCESS"),
BODY_NOT_MATCH("400", "请求的数据格式不符!", "BODY_NOT_MATCH"),
INTERNAL_SERVER_ERROR("500", "服务器内部错误!", "INTERNAL_SERVER_ERROR"),
INVALID_ACCOUNTID("406", "无效愉快办ID", "INVALID_ACCOUNTID"),
NOT_FOUND_ACCOUNTID("407", "愉快办ID必传但未传递", "NOT_FOUND_ACCOUNTID"),
NOT_FOUND_PROCINSID_FORMID_FORMTITLE("402", "流程定义id表单id工单标题优先级流程类型必传但未传递", "NOT_FOUND_PROCINSID_FORMID_FORMTITLE"),
NOT_FOUND_PROCINSID_DEPLOYID("408", "流程id部署id必传但未传递", "NOT_FOUND_PROCINSID_DEPLOYID"),
BODY_NOT_TIMESTAMP("400", "请求头未携带时间戳", "BODY_NOT_TIMESTAMP"),
BODY_NOT_SIGN("400", "请求头sign为空", "BODY_NOT_SIGN"),
BODY_NOT_APPID("400", "请求头appId为空,验签失败", "BODY_NOT_APPID"),
TIMEOUT("406", "验签失败,时间超过三分钟", "TIMEOUT"),
NOT_APPINFO("406", "未查询到相应信息,验签失败。", "NOT_APPINFO"),
NOT_APP_SECRET("406", "app_secret,查询为空,验签失败", "NOT_APP_SECRET"),
DIFFERENT_SIGN("406", "sign加密不一样,验签失败", "DIFFERENT_SIGN"),
SERVER_BUSY("503", "服务器正忙,请稍后再试!", "SERVER_BUSY");
/**
* 错误码
*/
private String resultCode;
/**
* 错误描述
*/
private String resultMsg;
/**
* 错误信息码
*/
private String message;
CommonEnum(String resultCode, String resultMsg, String message) {
this.resultCode = resultCode;
this.resultMsg = resultMsg;
this.message = message;
}
@Override
public String getResultCode() {
return resultCode;
}
@Override
public String getResultMsg() {
return resultMsg;
}
@Override
public String getMessage() {
return message;
}
}
public class ResultBody {
/**
* 响应代码
*/
private String code;
/**
* 响应消息
*/
private String message;
/**
* 响应消息码
*/
private String msg;
/**
* 响应结果
*/
private Object result;
public ResultBody() {
}
public ResultBody(BaseErrorInfoInterface errorInfo) {
this.code = errorInfo.getResultCode();
this.message = errorInfo.getResultMsg();
this.msg = errorInfo.getMessage();
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
/**
* 成功
*
* @return
*/
public static ResultBody success() {
return success(null);
}
/**
* 成功
*
* @param data
* @return
*/
public static ResultBody success(Object data) {
ResultBody rb = new ResultBody();
rb.setCode(CommonEnum.SUCCESS.getResultCode());
rb.setMessage(CommonEnum.SUCCESS.getResultMsg());
rb.setMsg(CommonEnum.SUCCESS.getMessage());
rb.setResult(data);
return rb;
}
/**
* 失败
*/
public static ResultBody error(BaseErrorInfoInterface errorInfo) {
ResultBody rb = new ResultBody();
rb.setCode(errorInfo.getResultCode());
rb.setMessage(errorInfo.getResultMsg());
rb.setMsg(errorInfo.getMessage());
rb.setResult(null);
return rb;
}
/**
* 失败
*/
public static ResultBody error(String code, String message, String msg) {
ResultBody rb = new ResultBody();
rb.setCode(code);
rb.setMessage(message);
rb.setMsg(msg);
rb.setResult(null);
return rb;
}
/**
* 失败
*/
public static ResultBody error(String message) {
ResultBody rb = new ResultBody();
rb.setCode("-1");
rb.setMessage(message);
rb.setResult(null);
return rb;
}
@Override
public String toString() {
return JSONObject.toJSONString(this);
}
}
完美解决
{
"code": "400",
"message": "请求头未携带时间戳",
"msg": "BODY_NOT_TIMESTAMP",
"result": null
}