Skip to content

Commit 6a6ab47

Browse files
committed
build filtered stack trace directly from throwable
#918 (comment)
1 parent 9abf498 commit 6a6ab47

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

src/main/java/com/conveyal/analysis/components/HttpApi.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import static com.conveyal.analysis.AnalysisServerException.Type.RUNTIME;
2727
import static com.conveyal.analysis.AnalysisServerException.Type.UNAUTHORIZED;
2828
import static com.conveyal.analysis.AnalysisServerException.Type.UNKNOWN;
29-
import static com.conveyal.r5.util.ExceptionUtils.filterStackTrace;
3029

3130
/**
3231
* This Component is a web server that serves up our HTTP API endpoints, contacted by both the UI and the workers.
@@ -180,7 +179,7 @@ private void respondToException(Exception e, Request request, Response response,
180179
// Include a stack trace except when the error is known to be about unauthenticated or unauthorized access,
181180
// in which case we don't want to leak information about the server to people scanning it for weaknesses.
182181
if (type != UNAUTHORIZED && type != FORBIDDEN) {
183-
body.put("stackTrace", filterStackTrace(errorEvent.stackTrace));
182+
body.put("stackTrace", errorEvent.filteredStackTrace);
184183
}
185184
response.status(code);
186185
response.type("application/json");

src/main/java/com/conveyal/analysis/components/eventbus/ErrorEvent.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
*/
1212
public class ErrorEvent extends Event {
1313

14-
// We may serialize this object, so we convert the Throwable to two strings to control its representation.
14+
// All Events are intended to be eligible for serialization into a log or database, so we convert the Throwable to
15+
// some Strings to determine its representation in a simple way.
1516
// For flexibility in event handlers, it is tempting to hold on to the original Throwable instead of derived
1617
// Strings. Exceptions are famously slow, but it's the initial creation and filling in the stack trace that are
17-
// slow. Once the instace exists, repeatedly examining its stack trace should not be prohibitively costly. Still,
18-
// we do probably gain some efficiency by converting the stack trace to a String once and reusing that.
18+
// slow. Once the instance exists, repeatedly examining its stack trace should not be prohibitively costly.
1919

2020
public final String summary;
2121

@@ -25,11 +25,16 @@ public class ErrorEvent extends Event {
2525
*/
2626
public final String httpPath;
2727

28+
/** The full stack trace of the exception that occurred. */
2829
public final String stackTrace;
2930

31+
/** A minimal stack trace showing the immediate cause within Conveyal code. */
32+
public final String filteredStackTrace;
33+
3034
public ErrorEvent (Throwable throwable, String httpPath) {
3135
this.summary = ExceptionUtils.shortCauseString(throwable);
3236
this.stackTrace = ExceptionUtils.stackTraceString(throwable);
37+
this.filteredStackTrace = ExceptionUtils.filterStackTrace(throwable);
3338
this.httpPath = httpPath;
3439
}
3540

@@ -56,7 +61,7 @@ public String traceWithContext (boolean verbose) {
5661
if (verbose) {
5762
builder.append(stackTrace);
5863
} else {
59-
builder.append(filterStackTrace(stackTrace));
64+
builder.append(filteredStackTrace);
6065
}
6166
return builder.toString();
6267
}

src/main/java/com/conveyal/r5/util/ExceptionUtils.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public static String shortAndLongString (Throwable throwable) {
5555
* and all additional frames that come from Conveyal packages. This yields a much shorter stack trace that still
5656
* shows where the exception was thrown and where the problem originates in our own code.
5757
*/
58-
public static String filterStackTrace (String stackTrace) {
59-
if (stackTrace == null) return null;
58+
public static String filterStackTrace (Throwable throwable) {
59+
String stackTrace = stackTraceString(throwable);
6060
final String unknownFrame = "Unknown stack frame, probably optimized out by JVM.";
6161
String error = stackTrace.lines().findFirst().get();
6262
String frame = stackTrace.lines()
@@ -71,8 +71,4 @@ public static String filterStackTrace (String stackTrace) {
7171
return String.join("\n", error, frame, conveyalFrame);
7272
}
7373

74-
public static String filterStackTrace (Throwable throwable) {
75-
return filterStackTrace(stackTraceString(throwable));
76-
}
77-
7874
}

0 commit comments

Comments
 (0)