This is an automated email from the ASF dual-hosted git repository.
remm pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/main by this push:
new 06c7046fd4 Add missing throwable to JsonErrorReportValve
06c7046fd4 is described below
commit 06c7046fd4d5208e084a84aff0ea487b00811d89
Author: remm <[email protected]>
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 6bd7a18e0e..4e06809b75 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -206,6 +206,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: [email protected]
For additional commands, e-mail: [email protected]