我的自定义 ExceptionHandler 不适用于 Ajax 请求

问题描述

我不知道为什么,但是我的自定义 ExceptionHandler 可以很好地处理普通请求,但不能处理 ajax 请求中的错误

这是我的 ExceptionHandler 的代码

public class YourExceptionHandler extends ExceptionHandlerWrapper {
    private ExceptionHandler wrapped;
    private Logger logger = Logger.getLogger(YourExceptionHandler.class.getName());

    public YourExceptionHandler(ExceptionHandler wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public void handle() throws FacesException {
        String errorPage = PageUtil.error;
        
        FacesContext context = FacesContext.getCurrentInstance();
        ExternalContext externalContext = context.getExternalContext();
        HttpSession session = (HttpSession) externalContext.getSession(false);
        
        HttpServletRequest req = (HttpServletRequest) externalContext.getRequest();
        String uri = req.getRequestURI();
        
        boolean ok = true;
        boolean logged = true;
        boolean loginPage = uri.endsWith("/login.xhtml");
        
        if (! loginPage) {
            if (session == null) {
                logged = false;
            }
            else {
                User user = (User ) session.getAttribute("user");
                
                if (user == null) {
                    logged = false;
                }
            }
        }
        
        if (logged) {
            Iterable<ExceptionQueuedEvent> queue = getUnhandledExceptionQueuedEvents();
            ExceptionQueuedEvent exceptionEvent;
            ExceptionQueuedEventContext exceptionEventContext;
            Throwable e;
            
            for (Iterator<ExceptionQueuedEvent> iterator = queue.iterator(); iterator.hasNext();) {
                try {
                    exceptionEvent =  iterator.next();
                    exceptionEventContext = exceptionEvent.getContext();
                    e = exceptionEventContext.getException();
                    
                    logger.error("Uncatched exception:\n" + ExceptionUtil.getStackTrace(e));
                    ok = false;
                }
                finally {
                    iterator.remove();
                }
            }
        }
        else {
            errorPage = "/session_error.jsf";
        }
        
        if (! ok || ! logged) {
            JsfUtil.renderPage(errorPage,context);
        }
        
        this.getWrapped().handle();
    }

    @Override
    public ExceptionHandler getWrapped() {
        return wrapped;
    }
}

JsfUtil.renderPage()代码

public static void renderPage(String page,FacesContext context) {
   Application app = context.getApplication();
   ViewHandler viewHandler = app.getViewHandler();
   UIViewRoot root = viewHandler.createView(context,page);
   
   context.setViewRoot(root);
   PartialViewContext partialViewContext = context.getPartialViewContext();
   partialViewContext.setRenderAll(true);
   context.renderResponse();
}

JS 控制台没有显示任何内容。请求的 Http 状态码为 200...

我使用的是 PrimeFaces 3.4.1

我已经在 ExceptionHandler 中设置了一个断点并等待会话超时。代码被正确执行,它根本不起作用,尽管我在 SO 上看到很多答案都说相反:(

我能做什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)