aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/process/ChildProcessException.java
blob: 852564e10a6ea6a5137e7e5a236bc5bc883ce836 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.vespa.hosted.node.admin.task.util.process;

import com.yahoo.text.internal.SnippetGenerator;

/**
 * Base class for child process related exceptions, with a util to build an error message
 * that includes a large part of the output.
 *
 * @author hakonhall
 */
@SuppressWarnings("serial")
public abstract class ChildProcessException extends RuntimeException {
    private static final SnippetGenerator snippetGenerator = new SnippetGenerator();

    /**
     * An exception with a message of the following format:
     *   Command 'COMMANDLINE' PROBLEM: stdout/stderr: 'OUTPUT'
     *
     * If the output of the terminated command is too large it will be sampled.
     *
     * @param problem            E.g. "terminated with exit code 1"
     * @param commandLine        The command that failed in a concise (e.g. shell-like) format
     * @param possiblyHugeOutput The output of the command
     */
    protected ChildProcessException(String problem, String commandLine, String possiblyHugeOutput) {
        super(makeSnippet(problem, commandLine, possiblyHugeOutput));
    }

    protected ChildProcessException(RuntimeException cause,
                                    String problem,
                                    String commandLine,
                                    String possiblyHugeOutput) {
        super(makeSnippet(problem, commandLine, possiblyHugeOutput), cause);
    }

    private static String makeSnippet(String problem, String commandLine, String possiblyHugeOutput) {
        return "Command '" +
                commandLine +
                "' " +
                problem +
                ": stdout/stderr: '" +
                snippetGenerator.makeSnippet(possiblyHugeOutput, 500) +
                "'";
    }
}