package com.hzya.frame.webapp.web.filter;

import com.hzya.frame.webapp.web.exception.CommonConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

/**
 * @Content异常处理filter
 * @Author 👻👻👻👻👻👻👻👻👻👻 gjh
 * @Date 2022-12-06 10:18
 * @Version 1.0
 */

public class ExceptionOptionFilter implements Filter {
    private final static Logger LOGGER = LoggerFactory.getLogger(ExceptionOptionFilter.class);

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        try {
            //LOGGER.info("第一过滤器：处理异常Filter");
            filterChain.doFilter(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            // 传递异常信息
            request.setAttribute("filterError", e);
            // 指定处理该请求的处理器
            request.getRequestDispatcher(CommonConstant.ERROR_CONTROLLER_PATH).forward(request, response);
        }
    }
}
