diff --git a/imixs-office-workflow-app/src/main/webapp/WEB-INF/faces-config.xml b/imixs-office-workflow-app/src/main/webapp/WEB-INF/faces-config.xml
index 222165e9..986d60c5 100644
--- a/imixs-office-workflow-app/src/main/webapp/WEB-INF/faces-config.xml
+++ b/imixs-office-workflow-app/src/main/webapp/WEB-INF/faces-config.xml
@@ -4,6 +4,11 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-facesconfig_3_0.xsd">
+
@@ -31,7 +31,7 @@
#{message['plugin_exception_title']}
@@ -40,7 +40,7 @@
#{message['login.conversation_expired_title']}
@@ -49,7 +49,7 @@
#{message['login.optimisticlock_exception_title']}
@@ -58,7 +58,7 @@
#{message.error_message_rollback_title}
@@ -76,13 +76,11 @@
-
#{errorHandlerData.exceptionType}: #{errorHandlerData.message}
+ #{flash.keep.type}: #{flash.keep.message}
+
+ Exception:#{flash.keep.exception}
- Exception: #{errorHandlerData.exception}
-
- Error Code:#{errorHandlerData.statusCode}
-
- URI:#{errorHandlerData.uri}
+ URI:#{flash.keep.uri}
{@code - *- * - * - * - * See also: https://www.baeldung.com/servlet-exceptions - * https://www.digitalocean.com/community/tutorials/servlet-exception-and-error-handling-example-tutorial - * - */ -@WebServlet(urlPatterns = "/errorHandler") -public class ErrorHandlerServlet extends HttpServlet { - - @Inject - private ErrorHandlerData errorHandlerData; - - @Override - protected void doGet(HttpServletRequest request, - HttpServletResponse response) throws ServletException, IOException { - processError(request, response); - } - - @Override - protected void doPost(HttpServletRequest request, - HttpServletResponse response) throws ServletException, IOException { - processError(request, response); - } - - private void processError(HttpServletRequest request, - HttpServletResponse response) throws IOException { - // Analyze the servlet exception - Throwable throwable = (Throwable) request - .getAttribute("jakarta.servlet.error.exception"); - Integer statusCode = (Integer) request - .getAttribute("jakarta.servlet.error.status_code"); - String servletName = (String) request - .getAttribute("jakarta.servlet.error.servlet_name"); - if (servletName == null) { - servletName = "Unknown"; - } - String requestUri = (String) request - .getAttribute("jakarta.servlet.error.request_uri"); - if (requestUri == null) { - requestUri = "Unknown"; - } - - // try { - if (throwable != null) { - errorHandlerData.setMessage(throwable.getMessage()); - errorHandlerData.setException(throwable.getClass().getName()); - } else { - errorHandlerData.setMessage("Unknown"); - - } - errorHandlerData.setUri(requestUri); - errorHandlerData.setStatusCode(statusCode); - - response.sendRedirect(request.getContextPath() + "/errorhandler.xhtml"); - - } - -} \ No newline at end of file diff --git a/imixs-office-workflow-util/src/main/java/org/imixs/workflow/office/util/ImixsExceptionHandler.java b/imixs-office-workflow-util/src/main/java/org/imixs/workflow/office/util/ImixsExceptionHandler.java new file mode 100644 index 00000000..f0f2810b --- /dev/null +++ b/imixs-office-workflow-util/src/main/java/org/imixs/workflow/office/util/ImixsExceptionHandler.java @@ -0,0 +1,82 @@ +package org.imixs.workflow.office.util; + +import java.util.Iterator; +import java.util.Objects; + +import jakarta.faces.FacesException; +import jakarta.faces.application.NavigationHandler; +import jakarta.faces.context.ExceptionHandler; +import jakarta.faces.context.ExceptionHandlerWrapper; +import jakarta.faces.context.ExternalContext; +import jakarta.faces.context.FacesContext; +import jakarta.faces.context.Flash; +import jakarta.faces.event.ExceptionQueuedEvent; +import jakarta.faces.event.ExceptionQueuedEventContext; +import jakarta.servlet.http.HttpServletRequest; + +public class ImixsExceptionHandler extends ExceptionHandlerWrapper { + + public ImixsExceptionHandler(ExceptionHandler wrapped) { + super(wrapped); + } + + @Override + public void handle() throws FacesException { + Iterator iterator = getUnhandledExceptionQueuedEvents().iterator(); + + while (iterator.hasNext()) { + ExceptionQueuedEvent event = (ExceptionQueuedEvent) iterator.next(); + ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource(); + + Throwable throwable = context.getException(); + + throwable = findCauseUsingPlainJava(throwable); + + FacesContext fc = FacesContext.getCurrentInstance(); + + try { + Flash flash = fc.getExternalContext().getFlash(); + + // Put the exception in the flash scope to be displayed in the error + // page if necessary ... + flash.put("message", throwable.getMessage()); + + ExternalContext externalContext = fc.getExternalContext(); + + flash.put("type", throwable.getClass().getSimpleName()); + flash.put("exception", throwable.getClass().getName()); + if (externalContext != null) { + HttpServletRequest hrequest = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext() + .getRequest(); + String uri = hrequest.getRequestURI(); + flash.put("uri", uri); + } + + NavigationHandler navigationHandler = fc.getApplication().getNavigationHandler(); + navigationHandler.handleNavigation(fc, null, "/errorhandler.xhtml?faces-redirect=true"); + fc.renderResponse(); + } finally { + iterator.remove(); + } + } + + // Let the parent handle the rest + getWrapped().handle(); + } + + /** + * Helper method to find the exception root cause. + * + * See: https://www.baeldung.com/java-exception-root-cause + */ + public static Throwable findCauseUsingPlainJava(Throwable throwable) { + Objects.requireNonNull(throwable); + Throwable rootCause = throwable; + while (rootCause.getCause() != null && rootCause.getCause() != rootCause) { + System.out.println("cause: " + rootCause.getCause().getMessage()); + rootCause = rootCause.getCause(); + } + return rootCause; + } + +} \ No newline at end of file diff --git a/imixs-office-workflow-util/src/main/java/org/imixs/workflow/office/util/ImixsExceptionHandlerFactory.java b/imixs-office-workflow-util/src/main/java/org/imixs/workflow/office/util/ImixsExceptionHandlerFactory.java new file mode 100644 index 00000000..174086b1 --- /dev/null +++ b/imixs-office-workflow-util/src/main/java/org/imixs/workflow/office/util/ImixsExceptionHandlerFactory.java @@ -0,0 +1,32 @@ +package org.imixs.workflow.office.util; + +import jakarta.faces.context.ExceptionHandler; +import jakarta.faces.context.ExceptionHandlerFactory; + +/** + * This is the custom exception handler factory class that is responsible for + * creating the instances of the ImixsExceptionHandler class. + * + * The factory need to be declared in the faces-config.xml file + * + *- * - * }java.lang.Exception - */errorHandler - *
{@code + *+ */ +public class ImixsExceptionHandlerFactory extends ExceptionHandlerFactory { + + public ImixsExceptionHandlerFactory(ExceptionHandlerFactory wrapped) { + super(wrapped); + } + + @Override + public ExceptionHandler getExceptionHandler() { + ExceptionHandler parentHandler = getWrapped().getExceptionHandler(); + return new ImixsExceptionHandler(parentHandler); + } + +} \ No newline at end of file+ + * }+ org.imixs.workflow.office.util.ImixsExceptionHandlerFactory + +