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

remm pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/9.0.x by this push:
     new eb1ce343e9 Add missing throwable to JsonErrorReportValve
eb1ce343e9 is described below

commit eb1ce343e96d5e5115eba998eeb3b82fe9287cff
Author: remm <r...@apache.org>
AuthorDate: Fri Mar 28 21:56:39 2025 +0100

    Add missing throwable to JsonErrorReportValve
---
 .../catalina/valves/JsonErrorReportValve.java      | 54 ++++++++++++++++++----
 webapps/docs/changelog.xml                         |  5 ++
 2 files changed, 50 insertions(+), 9 deletions(-)

diff --git a/java/org/apache/catalina/valves/JsonErrorReportValve.java 
b/java/org/apache/catalina/valves/JsonErrorReportValve.java
index 8d978a03f0..beb0b5866c 100644
--- a/java/org/apache/catalina/valves/JsonErrorReportValve.java
+++ b/java/org/apache/catalina/valves/JsonErrorReportValve.java
@@ -28,12 +28,8 @@ import org.apache.tomcat.util.json.JSONFilter;
 import org.apache.tomcat.util.res.StringManager;
 
 /**
- * <p>
- * Implementation of a Valve that outputs error jsons.
- * </p>
- * <p>
+ * Implementation of a Valve that outputs error JSON.
  * This Valve should be attached at the Host level, although it will work if 
attached to a Context.
- * </p>
  */
 public class JsonErrorReportValve extends ErrorReportValve {
 
@@ -85,9 +81,48 @@ public class JsonErrorReportValve extends ErrorReportValve {
                 description = 
smClient.getString("errorReportValve.noDescription");
             }
         }
-        String jsonReport = "{\n" + "  \"type\": \"" + JSONFilter.escape(type) 
+ "\",\n" + "  \"message\": \"" +
-                JSONFilter.escape(message) + "\",\n" + "  \"description\": \"" 
+ JSONFilter.escape(description) +
-                "\"\n" + "}";
+        StringBuilder sb = new StringBuilder();
+        sb.append("{\n  \"type\": 
\"").append(JSONFilter.escape(type)).append("\",\n");
+        sb.append("  \"message\": 
\"").append(JSONFilter.escape(message)).append("\",\n");
+        sb.append("  \"description\": 
\"").append(JSONFilter.escape(description));
+
+        if (throwable != null) {
+            sb.append("\",\n");
+
+            // Stack trace
+            sb.append("  \"throwable\": [");
+            boolean first = true;
+            do {
+                if (!first) {
+                    sb.append(',');
+                } else {
+                    first = false;
+                }
+                
sb.append('\"').append(JSONFilter.escape(throwable.toString())).append('\"');
+
+                StackTraceElement[] elements = throwable.getStackTrace();
+                int pos = elements.length;
+                for (int i = elements.length - 1; i >= 0; i--) {
+                    if 
((elements[i].getClassName().startsWith("org.apache.catalina.core.ApplicationFilterChain"))
 &&
+                            
(elements[i].getMethodName().equals("internalDoFilter"))) {
+                        pos = i;
+                        break;
+                    }
+                }
+                for (int i = 0; i < pos; i++) {
+                    if 
(!(elements[i].getClassName().startsWith("org.apache.catalina.core."))) {
+                        sb.append(',').append('\"').append(' 
').append(JSONFilter.escape(elements[i].toString())).append('\"');
+                    }
+                }
+
+                throwable = throwable.getCause();
+            } while (throwable != null);
+            sb.append("]\n}");
+
+        } else {
+            sb.append("\"\n}");
+        }
+
         try {
             try {
                 response.setContentType("application/json");
@@ -100,11 +135,12 @@ public class JsonErrorReportValve extends 
ErrorReportValve {
             }
             Writer writer = response.getReporter();
             if (writer != null) {
-                writer.write(jsonReport);
+                writer.write(sb.toString());
                 response.finishResponse();
             }
         } catch (IOException | IllegalStateException e) {
             // Ignore
         }
     }
+
 }
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 0844f2003d..286514ae3e 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -156,6 +156,11 @@
         <bug>69634</bug>: Avoid NPE on <code>JsonErrorReportValve</code>.
         (remm)
       </fix>
+      <fix>
+        Add missing <code>throwable</code> stack trace to
+        <code>JsonErrorReportValve</code> equivalent to the one from
+        <code>ErrorReportValve</code>. (remm)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Coyote">


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to