You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2022/05/09 10:12:30 UTC

[sling-org-apache-sling-servlets-resolver] branch master updated: SLING-11307 : Different exception passed to error handler with bundled scripts

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

cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-servlets-resolver.git


The following commit(s) were added to refs/heads/master by this push:
     new bf17678  SLING-11307 : Different exception passed to error handler  with bundled scripts
bf17678 is described below

commit bf17678d4d245bd0cba39c9bc2eba3acbb6bd57d
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Mon May 9 12:12:22 2022 +0200

    SLING-11307 : Different exception passed to error handler  with bundled scripts
---
 .../internal/bundle/BundledScriptServlet.java      | 41 +++++++++++++++++++---
 1 file changed, 37 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/apache/sling/servlets/resolver/internal/bundle/BundledScriptServlet.java b/src/main/java/org/apache/sling/servlets/resolver/internal/bundle/BundledScriptServlet.java
index d6a77d4..66968c9 100644
--- a/src/main/java/org/apache/sling/servlets/resolver/internal/bundle/BundledScriptServlet.java
+++ b/src/main/java/org/apache/sling/servlets/resolver/internal/bundle/BundledScriptServlet.java
@@ -24,15 +24,19 @@ import java.util.Collection;
 import java.util.Set;
 import java.util.stream.Collectors;
 
+import javax.script.ScriptException;
 import javax.servlet.GenericServlet;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
 
 import org.apache.sling.api.SlingConstants;
+import org.apache.sling.api.SlingException;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.SlingServletException;
 import org.apache.sling.api.resource.type.ResourceType;
+import org.apache.sling.api.scripting.ScriptEvaluationException;
 import org.apache.sling.scripting.spi.bundle.BundledRenderUnit;
 import org.apache.sling.scripting.spi.bundle.TypeProvider;
 import org.jetbrains.annotations.NotNull;
@@ -76,16 +80,45 @@ public class BundledScriptServlet extends GenericServlet {
             RequestWrapper requestWrapper = new RequestWrapper(request, types);
             try {
                 executable.eval(requestWrapper, response);
-            } catch (Exception se) {
-                Throwable cause = (se.getCause() == null) ? se : se.getCause();
-                throw cause instanceof ServletException ? (ServletException) cause :
-                        new ServletException(String.format("Failed executing script %s: %s", executable.getPath(), se.getMessage()), cause);
+            } catch (ScriptEvaluationException see) {
+
+                // log in the request progress tracker
+                logScriptError(request, see);
+    
+                throw see;
+            } catch (SlingException e) {
+                // log in the request progress tracker
+                logScriptError(request, e);
+    
+                throw e;
+            } catch (Exception e) {
+    
+                // log in the request progress tracker
+                logScriptError(request, e);
+    
+                throw new SlingException("Cannot get DefaultSlingScript: "
+                    + e.getMessage(), e);
             }
         } else {
             throw new ServletException("Not a Sling HTTP request/response");
         }
     }
 
+    /**
+     * Logs the error caused by executing the script in the request progress
+     * tracker.
+     */
+    private void logScriptError(SlingHttpServletRequest request,
+            Throwable throwable) {
+        String message = throwable.getMessage();
+        if (message != null) {
+            message = throwable.getMessage().replace('\n', '/');
+        } else {
+            message = throwable.toString();
+        }
+        request.getRequestProgressTracker().log("SCRIPT ERROR: {0}", message);
+    }
+
     @NotNull
     public BundledRenderUnit getBundledRenderUnit() {
         return executable;