自定义的filter抛出异常不走全局自定义异常


问题描述

提示:这里描述项目中遇到的问题:

自定义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
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
如果`doGetAuthenticationInfo`方法抛出的是一个自定义异常,并且该异常没有被捕获,那么它会沿着调用栈一直抛出,直到被JVM捕获或导致应用程序崩溃。为了避免这种情况,可以使用全局异常处理器来捕获并处理`doGetAuthenticationInfo`方法抛出的自定义异常。 在Java,可以通过实现`javax.servlet.Filter`接口并在`web.xml`文件配置来实现全局异常处理器。例如: ```java public class GlobalExceptionHandler implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { try { filterChain.doFilter(servletRequest, servletResponse); } catch (MyException e) { // 处理自定义异常 e.printStackTrace(); } catch (Exception e) { // 处理其他异常 e.printStackTrace(); } } // 省略其他方法 } ``` 在`doFilter`方法,可以使用try-catch块来捕获自定义异常(例如`MyException`),并进行相应的处理。如果该异常没有被捕获,将会继续抛出,直到被全局异常处理器捕获。 然后,在`web.xml`文件配置全局异常处理器: ```xml <filter> <filter-name>GlobalExceptionHandler</filter-name> <filter-class>com.example.GlobalExceptionHandler</filter-class> </filter> <filter-mapping> <filter-name>GlobalExceptionHandler</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ``` 上述配置将会拦截所有的请求,并使用`GlobalExceptionHandler`来处理异常。这样,当`doGetAuthenticationInfo`方法抛出自定义异常时,就能够被全局异常处理器捕获并处理,而不会导致应用程序崩溃。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是小余同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值