You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ta...@apache.org on 2024/12/18 17:32:05 UTC

(myfaces) branch 4.0.x updated: MYFACES-4701 - Add ability to skip logging of specific exceptions in the ExceptionHandler impl

This is an automated email from the ASF dual-hosted git repository.

tandraschko pushed a commit to branch 4.0.x
in repository https://gitbox.apache.org/repos/asf/myfaces.git


The following commit(s) were added to refs/heads/4.0.x by this push:
     new 769dab80b MYFACES-4701 - Add ability to skip logging of specific exceptions in the ExceptionHandler impl
769dab80b is described below

commit 769dab80b64657a1224af410f881ffa1b832dc15
Author: tandraschko <ta...@apache.org>
AuthorDate: Wed Dec 18 17:51:16 2024 +0100

    MYFACES-4701 - Add ability to skip logging of specific exceptions in the ExceptionHandler impl
---
 .../config/webparameters/MyfacesConfig.java        | 29 +++++++-
 .../myfaces/context/AjaxExceptionHandlerImpl.java  | 11 +--
 .../myfaces/context/ExceptionHandlerImpl.java      | 25 +++----
 .../myfaces/context/ExceptionHandlerUtils.java     | 82 +++++++++++++++++++---
 .../MyFacesExceptionHandlerWrapperImpl.java        |  5 +-
 5 files changed, 114 insertions(+), 38 deletions(-)

diff --git a/impl/src/main/java/org/apache/myfaces/config/webparameters/MyfacesConfig.java b/impl/src/main/java/org/apache/myfaces/config/webparameters/MyfacesConfig.java
index 2f8905fed..30b3e3e2d 100755
--- a/impl/src/main/java/org/apache/myfaces/config/webparameters/MyfacesConfig.java
+++ b/impl/src/main/java/org/apache/myfaces/config/webparameters/MyfacesConfig.java
@@ -18,6 +18,8 @@
  */
 package org.apache.myfaces.config.webparameters;
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.ResourceBundle;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -790,6 +792,10 @@ public class MyfacesConfig
     public static final String EL_RESOLVER_TRACING = "org.apache.myfaces.EL_RESOLVER_TRACING";
     public static final boolean EL_RESOLVER_TRACING_DEFAULT = false;
 
+    @JSFWebConfigParam(name="org.apache.myfaces.EXCEPTION_TYPES_TO_IGNORE_IN_LOGGING", since="4.0.3, 4.1.1, 5.0", defaultValue = "")
+    public static final String EXCEPTION_TYPES_TO_IGNORE_IN_LOGGING =
+            "org.apache.myfaces.EXCEPTION_TYPES_TO_IGNORE_IN_LOGGING";
+
     // we need it, applicationImpl not ready probably
     private ProjectStage projectStage = ProjectStage.Production;
     private boolean strictJsf2AllowSlashLibraryName;
@@ -869,6 +875,7 @@ public class MyfacesConfig
     private ResourceBundle.Control resourceBundleControl;
     private boolean automaticExtensionlessMapping = AUTOMATIC_EXTENSIONLESS_MAPPING_DEFAULT;
     private boolean elResolverTracing = EL_RESOLVER_TRACING_DEFAULT;
+    private List<String> exceptionTypesToIgnoreInLogging = new ArrayList<>();
     
     private static final boolean MYFACES_IMPL_AVAILABLE;
     private static final boolean RI_IMPL_AVAILABLE;
@@ -960,7 +967,7 @@ public class MyfacesConfig
         cfg.delegateFacesServlet = getString(extCtx, DELEGATE_FACES_SERVLET,
                 null);
         
-        String refreshTransientBuildOnPSS = getString(extCtx, REFRESH_TRANSIENT_BUILD_ON_PSS, 
+        String refreshTransientBuildOnPSS = getString(extCtx, REFRESH_TRANSIENT_BUILD_ON_PSS,
                 REFRESH_TRANSIENT_BUILD_ON_PSS_DEFAULT);
         if (refreshTransientBuildOnPSS == null)
         {
@@ -1228,7 +1235,7 @@ public class MyfacesConfig
         cfg.fullStateSavingViewIds = StringUtils.splitShortString(
                 getString(extCtx, StateManager.FULL_STATE_SAVING_VIEW_IDS_PARAM_NAME, null),
                 ',');
-        
+
         cfg.faceletsBufferSize = getInt(extCtx, ViewHandler.FACELETS_BUFFER_SIZE_PARAM_NAME,
                 1024);
         
@@ -1309,7 +1316,18 @@ public class MyfacesConfig
 
         cfg.elResolverTracing = getBoolean(extCtx, EL_RESOLVER_TRACING,
                 EL_RESOLVER_TRACING_DEFAULT);
-        
+
+        String[] exceptionTypesToIgnoreInLogging = StringUtils.splitShortString(
+                getString(extCtx, EXCEPTION_TYPES_TO_IGNORE_IN_LOGGING, ""),
+                ',');
+        for (String exceptionTypeToIgnoreInLogging : exceptionTypesToIgnoreInLogging)
+        {
+            if (StringUtils.isNotBlank(exceptionTypeToIgnoreInLogging))
+            {
+                cfg.exceptionTypesToIgnoreInLogging.add(exceptionTypeToIgnoreInLogging);
+            }
+        }
+
         return cfg;
     }
 
@@ -1774,5 +1792,10 @@ public class MyfacesConfig
     {
         return elResolverTracing;
     }
+
+    public List<String> getExceptionTypesToIgnoreInLogging()
+    {
+        return exceptionTypesToIgnoreInLogging;
+    }
 }
 
diff --git a/impl/src/main/java/org/apache/myfaces/context/AjaxExceptionHandlerImpl.java b/impl/src/main/java/org/apache/myfaces/context/AjaxExceptionHandlerImpl.java
index e878cdcac..a82a9dcb9 100644
--- a/impl/src/main/java/org/apache/myfaces/context/AjaxExceptionHandlerImpl.java
+++ b/impl/src/main/java/org/apache/myfaces/context/AjaxExceptionHandlerImpl.java
@@ -165,15 +165,16 @@ public class AjaxExceptionHandlerImpl extends ExceptionHandler
                     // jakarta.faces.event.AbortProcessingException
                     if (!shouldSkip(exception))
                     {
+                        Throwable rootCause = getRootCause(exception);
+
                         // set handledAndThrown so that getHandledExceptionQueuedEvent() returns this event
                         handledAndThrown = event;
-                        
-                        Throwable rootCause = getRootCause(exception);
-                        
+
                         throwableList.add(rootCause == null ? exception : rootCause);
-                    }
 
-                    ExceptionHandlerUtils.logException(context, log);
+                        ExceptionHandlerUtils.logException(rootCause == null ? exception : rootCause,
+                                context.getComponent(), context.getContext(), log);
+                    }
                 }
                 finally
                 {
diff --git a/impl/src/main/java/org/apache/myfaces/context/ExceptionHandlerImpl.java b/impl/src/main/java/org/apache/myfaces/context/ExceptionHandlerImpl.java
index a18ccd2d9..5b4cd96ec 100644
--- a/impl/src/main/java/org/apache/myfaces/context/ExceptionHandlerImpl.java
+++ b/impl/src/main/java/org/apache/myfaces/context/ExceptionHandlerImpl.java
@@ -125,13 +125,13 @@ public class ExceptionHandlerImpl extends ExceptionHandler
                     
                     // and call getException() on the returned result
                     Throwable exception = context.getException();
-                    
-                    ExceptionHandlerUtils.logException(context, log);
 
                     // Upon encountering the first such Exception that is not an instance of
                     // jakarta.faces.event.AbortProcessingException
                     if (!shouldSkip(exception))
                     {
+                        Throwable rootCause = getRootCause(exception);
+
                         // set handledAndThrown so that getHandledExceptionQueuedEvent() returns this event
                         handledAndThrown = event;
 
@@ -139,7 +139,11 @@ public class ExceptionHandlerImpl extends ExceptionHandler
                         // and throw it
                         // FIXME: The spec says to NOT use a FacesException to propagate the exception, but I see
                         //        no other way as ServletException is not a RuntimeException
-                        toThrow = wrap(getRethrownException(exception));
+                        toThrow = wrap(rootCause == null ? exception : rootCause);
+
+                        ExceptionHandlerUtils.logException(rootCause == null ? exception : rootCause,
+                                context.getComponent(), context.getContext(), log);
+
                         break;
                     }
                 }
@@ -188,20 +192,7 @@ public class ExceptionHandlerImpl extends ExceptionHandler
         
         unhandled.add((ExceptionQueuedEvent)exceptionQueuedEvent);
     }
-    
-    protected Throwable getRethrownException(Throwable exception)
-    {
-        // Let toRethrow be either the result of calling getRootCause() on the Exception, 
-        // or the Exception itself, whichever is non-null
-        Throwable toRethrow = getRootCause(exception);
-        if (toRethrow == null)
-        {
-            toRethrow = exception;
-        }
-        
-        return toRethrow;
-    }
-    
+
     protected FacesException wrap(Throwable exception)
     {
         if (exception instanceof FacesException)
diff --git a/impl/src/main/java/org/apache/myfaces/context/ExceptionHandlerUtils.java b/impl/src/main/java/org/apache/myfaces/context/ExceptionHandlerUtils.java
index 02b97c487..661ec8af7 100644
--- a/impl/src/main/java/org/apache/myfaces/context/ExceptionHandlerUtils.java
+++ b/impl/src/main/java/org/apache/myfaces/context/ExceptionHandlerUtils.java
@@ -18,34 +18,93 @@
  */
 package org.apache.myfaces.context;
 
+import jakarta.el.ELException;
+import jakarta.faces.FacesException;
 import jakarta.faces.FacesWrapper;
+import jakarta.faces.application.ProjectStage;
+import jakarta.faces.application.ViewExpiredException;
 import jakarta.faces.component.UIComponent;
-import jakarta.faces.event.ExceptionQueuedEventContext;
-import java.util.logging.Level;
-import java.util.logging.Logger;
+import jakarta.faces.context.FacesContext;
+import org.apache.myfaces.config.webparameters.MyfacesConfig;
 import org.apache.myfaces.view.facelets.LocationAware;
 import org.apache.myfaces.view.facelets.el.ContextAware;
 
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
 public class ExceptionHandlerUtils
 {
+    protected static boolean isLogStacktrace(FacesContext context, Throwable exception)
+    {
+        if (context.isProjectStage(ProjectStage.Production))
+        {
+            if (exception instanceof ViewExpiredException)
+            {
+                return false;
+            }
+        }
+
+        return true;
+    }
 
-    public static void logException(ExceptionQueuedEventContext event, Logger logger)
+    protected static boolean isLogException(FacesContext context, Throwable exception)
     {
-        // unwrap to strip location aware stacktraces
-        Throwable exception = event.getException();
-        while (exception instanceof FacesWrapper)
+        if (context.isProjectStage(ProjectStage.Production))
+        {
+            if (exception != null)
+            {
+                MyfacesConfig myfacesConfig = MyfacesConfig.getCurrentInstance(context);
+                for (String ignore : myfacesConfig.getExceptionTypesToIgnoreInLogging())
+                {
+                    if (ignore.trim().equals(exception.getClass().getName()))
+                    {
+                        return false;
+                    }
+                }
+            }
+        }
+
+        return true;
+    }
+
+    public static void logException(Throwable exception, UIComponent component, FacesContext context, Logger logger)
+    {
+        while (exception instanceof FacesWrapper ||
+                (exception.getClass().equals(FacesException.class) || exception.getClass().equals(ELException.class)))
+        {
+            if (exception instanceof FacesWrapper)
+            {
+                exception = (Throwable) ((FacesWrapper) exception).getWrapped();
+            }
+            else if (exception.getClass().equals(FacesException.class)
+                    || exception.getClass().equals(ELException.class))
+            {
+                exception = exception.getCause();
+            }
+        }
+
+        if (!isLogException(context, exception))
         {
-            exception = (Throwable) ((FacesWrapper) exception).getWrapped();
+            return;
         }
 
-        String msg = exception.getClass().getName() + " occured while processing " + event.getPhaseId().getName();
-        String location = buildLocation(event.getException(), event.getComponent());
+        String msg = exception.getClass().getName() + " occurred while processing "
+                + context.getCurrentPhaseId().getName();
+
+        String location = buildLocation(exception, component);
         if (location != null)
         {
             msg += " [Location=" + location + "]";
         }
 
-        logger.log(Level.SEVERE, msg, exception);
+        if (isLogStacktrace(context, exception))
+        {
+            logger.log(Level.SEVERE, msg, exception);
+        }
+        else
+        {
+            logger.log(Level.SEVERE, msg);
+        }
     }
 
     public static String buildLocation(Throwable ex, UIComponent component)
@@ -67,6 +126,7 @@ public class ExceptionHandlerUtils
             }
         }
 
+        // unwrap to strip location aware stacktraces
         while (ex.getCause() != null)
         {
             ex = ex.getCause();
diff --git a/impl/src/main/java/org/apache/myfaces/context/MyFacesExceptionHandlerWrapperImpl.java b/impl/src/main/java/org/apache/myfaces/context/MyFacesExceptionHandlerWrapperImpl.java
index 5914b0bcf..bd19771b5 100644
--- a/impl/src/main/java/org/apache/myfaces/context/MyFacesExceptionHandlerWrapperImpl.java
+++ b/impl/src/main/java/org/apache/myfaces/context/MyFacesExceptionHandlerWrapperImpl.java
@@ -243,8 +243,6 @@ public class MyFacesExceptionHandlerWrapperImpl extends ExceptionHandlerWrapper
                         
                         // and call getException() on the returned result
                         Throwable exception = context.getException();
-                        
-                        ExceptionHandlerUtils.logException(context, log);
 
                         // Upon encountering the first such Exception that is not an instance of
                         // jakarta.faces.event.AbortProcessingException
@@ -257,6 +255,9 @@ public class MyFacesExceptionHandlerWrapperImpl extends ExceptionHandlerWrapper
                             
                             throwableList.add(rootCause == null ? exception : rootCause);
                             components.add(event.getContext().getComponent());
+
+                            ExceptionHandlerUtils.logException(rootCause == null ? exception : rootCause,
+                                    context.getComponent(), context.getContext(), log);
                         }
                     }
                     finally