I don't know why, but my custom ExceptionHandler works great with normal requests, but not with errors in ajax requests.
This is the code of my 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;
}
}
Code of 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 Console shows me nothing. Http Status code of the request is 200...
I'm using PrimeFaces 3.4.1
I've done a breakpoint inside the ExceptionHandler and waited the session timeout. Code was executed correctly, it simply not works, despite many answer I see on SO that says the contrary :(
What can I do?