aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/ExceptionWrapper.java
blob: 75b07678d5b486ae97196d2e9c3b04448bd53f81 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright Vespa.ai. 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 Steinar Knutsen
 */
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;
    }
}