aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/ExceptionWrapper.java
diff options
context:
space:
mode:
Diffstat (limited to 'container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/ExceptionWrapper.java')
-rw-r--r--container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/ExceptionWrapper.java59
1 files changed, 59 insertions, 0 deletions
diff --git a/container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/ExceptionWrapper.java b/container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/ExceptionWrapper.java
new file mode 100644
index 00000000000..ebc10482600
--- /dev/null
+++ b/container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/ExceptionWrapper.java
@@ -0,0 +1,59 @@
+// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.jdisc.http.server.jetty;
+
+/**
+ * A wrapper to make exceptions leaking into Jetty easier to track. Jetty
+ * swallows all information about where an exception was thrown, so this wrapper
+ * ensures some extra information is automatically added to the contents of
+ * getMessage().
+ *
+ * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
+ */
+public class ExceptionWrapper extends RuntimeException {
+ private final String message;
+
+ /**
+ * Update if serializable contents are added.
+ */
+ private static final long serialVersionUID = 1L;
+
+ public ExceptionWrapper(Throwable t) {
+ super(t);
+ this.message = formatMessage(t);
+ }
+
+ // If calling methods from the constructor, it makes life easier if the
+ // methods are static...
+ private static String formatMessage(final Throwable t) {
+ StringBuilder b = new StringBuilder();
+ Throwable cause = t;
+ while (cause != null) {
+ StackTraceElement[] trace = cause.getStackTrace();
+ String currentMsg = cause.getMessage();
+
+ if (b.length() > 0) {
+ b.append(": ");
+ }
+ b.append(t.getClass().getSimpleName()).append('(');
+ if (currentMsg != null) {
+ b.append('"').append(currentMsg).append('"');
+ }
+ b.append(')');
+ if (trace.length > 0) {
+ b.append(" at ").append(trace[0].getClassName()).append('(');
+ if (trace[0].getFileName() != null) {
+ b.append(trace[0].getFileName()).append(':')
+ .append(trace[0].getLineNumber());
+ }
+ b.append(')');
+ }
+ cause = cause.getCause();
+ }
+ return b.toString();
+ }
+
+ @Override
+ public String getMessage() {
+ return message;
+ }
+}