



<error-page> <exception-type>javax.faces.application.ViewExpiredException</exception-type> <location>/viewExpired.xhtml</location> </error-page>

<factory> <exception-handler-factory>info.galleria.handlers.GalleriaExceptionHandlerFactory</exception-handler-factory> </factory>

@Override public ExceptionHandler getExceptionHandler() { ExceptionHandler result = parent.getExceptionHandler(); result = new GalleriaExceptionHandler(result); return result; }

@Override public void handle() throws FacesException { for (Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();) { ExceptionQueuedEvent event = i.next(); ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource(); Throwable t = context.getException(); if (t instanceof ViewExpiredException) { ViewExpiredException vee = (ViewExpiredException) t; FacesContext fc = FacesContext.getCurrentInstance(); Map<String, Object> requestMap = fc.getExternalContext().getRequestMap(); NavigationHandler nav = fc.getApplication().getNavigationHandler(); try { // Push some stuff to the request scope for later use in the page requestMap.put("currentViewId", vee.getViewId()); nav.handleNavigation(fc, null, "viewExpired"); fc.renderResponse(); } finally { i.remove(); } } } // Let the parent handle all the remaining queued exception events. getWrapped().handle(); }

<?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <ui:composition xmlns:ui="http://java.sun.com/jsf/facelets" template="./templates/defaultLayout.xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" > <ui:define name="title"> <h:outputText value="#{msg['Exception.page.title']}" /> </ui:define> <ui:define name="content"> <h:form> <h:outputText value="#{msg['Exception.page.message']}" /> <p>You were on page #{currentViewId}. Maybe that's useful.</p> <p>Please re-login via the <h:outputLink styleClass="homepagelink" value="#{request.contextPath}/Index.xhtml" ><h:outputText value="Homepage" /></h:outputLink>.</p> </h:form> </ui:define> </ui:composition>

// check for known Exceptions if (t instanceof ViewExpiredException) { ViewExpiredException vee = (ViewExpiredException) t; // Push some stuff to the request scope for later use in the page requestMap.put("currentViewId", vee.getViewId()); } else { forwardView = "generalError"; Locale locale = fc.getViewRoot().getLocale(); String key = "Excepetion.GeneralError"; logger.error(Messages.getLoggerString(key), t); String message = Messages.getString(key, locale); FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null); fc.addMessage(null, facesMessage); }

<dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency>

} else { forwardView = "generalError"; // no known instance try to specify Throwable causingEx = ExceptionUtils.getRootCause(t); if (causingEx == null) { causingEx = t; }

//... logger.error(Messages.getLoggerString(key), t); requestMap.put("errorCode", errorCode);

String errorCode = String.valueOf(Math.abs(new Date().hashCode()));

Exception.generalError.log=General Error logged: {}.

Exception.generalError.message=A general error with id {0} occured. Please call our hotline.

<h:outputFormat value="#{msg['Exception.generalError.message']}" > <f:param value="#{errorCode}"/> </h:outputFormat>

public static boolean isProduction() { ProjectStage stage = ProjectStage.Development; String stageValue = null; try { InitialContext ctx = new InitialContext(); stageValue = (String) ctx.lookup(ProjectStage.PROJECT_STAGE_JNDI_NAME); stage = ProjectStage.valueOf(stageValue); } catch (NamingException | IllegalArgumentException | NullPointerException e) { logger.error("Could not lookup JNDI object with name 'javax.faces.PROJECT_STAGE'. Using default 'production'"); } return ProjectStage.Production == stage; }

<error-page> <error-code>404</error-code> <location>/404.xhtml</location> </error-page>