Skip to content

Commit

Permalink
Rework Exception Message
Browse files Browse the repository at this point in the history
Now we always prefix the output with the stream it comes from.
Also, there is now indentation + an explicit visualisation oft he
command line.

Here is an example:
Exception in thread "main" org.buildobjects.process.ExternalProcessFailureException: External process `git` terminated with unexpected exit status 1 after 39ms:
  $ git commit -m '1st commit'
  STDOUT: On branch master
  STDOUT:
  STDOUT: Initial commit
  STDOUT:
  STDOUT: nothing to commit (create/copy files and use "git add" to track)
  • Loading branch information
fleipold committed Feb 21, 2021
1 parent c51bd4a commit 942e4be
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,32 @@
* such as the output on stderr.
*/
public class ExternalProcessFailureException extends RuntimeException {
final private String command;
private final String command;
final private String commandLine;
final private int exitValue;
final private String stderr;
private final ByteArrayOutputStream stdout;
final private long time;

ExternalProcessFailureException(String command, int exitValue, String stderr, ByteArrayOutputStream stdOut, long time) {
ExternalProcessFailureException(String command, String commandLine, int exitValue, String stderr, ByteArrayOutputStream stdOut, long time) {
this.command = command;
this.commandLine = commandLine;
this.exitValue = exitValue;
this.stderr = stderr;
this.stdout = stdOut;
this.time = time;
}

private String formatOutput(String string, String prefix) {
if (string == null) {
return prefix + "<Has already been consumed.>\n";
}
if (string.isEmpty()) {
return "";
}
return prefixLines(string, prefix);
}

private String prefixLines(String string, String prefix) {
StringBuilder builder = new StringBuilder();
String[] lines = string.split("\n");
Expand All @@ -33,29 +45,36 @@ private String prefixLines(String string, String prefix) {

@Override
public String getMessage() {
String formattedStdErr = stderr != null ? stderr : "Stderr unavailable as it has been consumed by user provided stream.";
String formattedStdOut = stdout != null ? new String(stdout.toByteArray(), UTF_8) : "";
if (!formattedStdErr.isEmpty() && !formattedStdOut.isEmpty()) {
formattedStdErr = prefixLines(formattedStdErr, "STDERR: ");
formattedStdOut = prefixLines(formattedStdOut, "STDOUT: ");
}
String formattedStdErr = formatOutput(stderr, " STDERR: ");
final String outString = stdout != null ? new String(stdout.toByteArray(), UTF_8) : null;
String formattedStdOut = formatOutput(outString, " STDOUT: ");

return
"External process '" + command +
"' returned " + exitValue +
" after " + time + "ms\n" +
"External process `" + command + "` terminated with unexpected exit status " + exitValue +
" after " + time + "ms:\n" +
" $ " + commandLine + "\n" +
formattedStdErr +
formattedStdOut;

}

/**
* @return the command that was executed
* @deprecated Use getCommandLine
*/
@Deprecated
public String getCommand() {
return command;
return commandLine;
}

/**
* @return a command line to invoke this process including args and using basic shell escaping.
*/
public String getCommandLine() {
return commandLine;
}



/**
* @return the actual exit value
*/
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/buildobjects/process/Proc.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,10 @@ private String[] concatenateCmdArgs() {

@Override
public String toString() {
return formatCommand(command, args);
return formatCommandLine(command, args);
}

static String formatCommand(String command, List<String> args) {
static String formatCommandLine(String command, List<String> args) {
return command + " " + argsString(args);
}

Expand Down
23 changes: 20 additions & 3 deletions src/main/java/org/buildobjects/process/ProcBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,9 @@ public ProcResult run() throws StartupException, TimeoutException, ExternalProce
final ByteArrayOutputStream output = defaultStdout == stdout && outputConsumer == null ? defaultStdout : null;

if (expectedExitStatuses.size() > 0 && !expectedExitStatuses.contains(proc.getExitValue())) {
throw new ExternalProcessFailureException(proc.toString(), proc.getExitValue(), proc.getErrorString(), output, proc.getExecutionTime());
throw new ExternalProcessFailureException(command, proc.toString(), proc.getExitValue(), proc.getErrorString(), output, proc.getExecutionTime());
}


return new ProcResult(proc.toString(), output, proc.getExitValue(), proc.getExecutionTime(), proc.getErrorBytes());
} finally {
stdout = defaultStdout = new ByteArrayOutputStream();
Expand Down Expand Up @@ -282,8 +281,26 @@ public ProcBuilder withOutputConsumer(StreamConsumer outputConsumer) {
* Also, this returns a representation of the current state of
* the builder. If more arguments are added the process this
* representation will not represent the process that gets launched.
*
* @deprecated Use getCommandLine instead.
*/
@Deprecated
public String getProcString() {
return Proc.formatCommand(command, args);
return Proc.formatCommandLine(command, args);
}

/** @return a string representation of the process invocation.
*
* This approximates the representation of this invocation
* in a shell. Note that the escaping of arguments is incomplete,
* it works only for whitespace. Fancy control characters are
* not replaced.
*
* Also, this returns a representation of the current state of
* the builder. If more arguments are added the process this
* representation will not represent the process that gets launched.
*/
public String getCommandLine() {
return Proc.formatCommandLine(command, args);
}
}
15 changes: 13 additions & 2 deletions src/main/java/org/buildobjects/process/ProcResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class ProcResult {
private final long executionTime;
private final byte[] err;


ProcResult(String procString, ByteArrayOutputStream output, int exitValue, long executionTime, byte[] err) {
this.procString = procString;
this.output = output;
Expand All @@ -29,11 +28,24 @@ public class ProcResult {
* in a shell. Note that the escaping of arguments is incomplete,
* it works only for whitespace. Fancy control characters are
* not replaced.
*
* @deprecated Use #getCommandLine() instead.
*/
@Deprecated
public String getProcString() {
return procString;
}

/** @return a string representation of the process invocation.
*
* This approximates the representation of this invocation
* in a shell. Note that the escaping of arguments is incomplete,
* it works only for whitespace. Fancy control characters are
* not replaced.
*/
public String getCommandLine() {
return procString;
}

private ByteArrayOutputStream getOutputStream() throws IllegalStateException {
if (output == null) {
Expand All @@ -42,7 +54,6 @@ private ByteArrayOutputStream getOutputStream() throws IllegalStateException {
return output;
}


/** @return the standard output as string
* @throws IllegalStateException if an OutputStream has been provided to captured the output */
public String getOutputString() throws IllegalStateException {
Expand Down
23 changes: 15 additions & 8 deletions src/test/java/org/buildobjects/process/ProcBuilderTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.buildobjects.process;

import org.junit.Ignore;
import org.junit.Test;

import java.io.*;
Expand Down Expand Up @@ -240,7 +239,7 @@ public void testNonZeroResultYieldsException() {
} catch (ExternalProcessFailureException ex) {
assertEquals("No such file or directory", ex.getStderr().split("\\:")[2].trim());
assertTrue(ex.getExitValue() > 0);
assertEquals("ls xyz", ex.getCommand());
assertEquals("ls xyz", ex.getCommandLine());
assertTrue(ex.getTime() > 0);

}
Expand All @@ -257,7 +256,11 @@ public void testNonZeroResultYieldsExceptionWithOnlyStdout() {
} catch (ExternalProcessFailureException ex) {
assertTrue(ex.getExitValue() == 1);
assertTrue(ex.getTime() > 0);
assertEquals("Standard Out", ex.getMessage().split("\n")[1].trim());

final String[] messageLines = ex.getMessage().split("\n");
assertEquals("External process `bash` terminated with unexpected exit status 1 after X:", messageLines[0].replaceAll("\\d+ms","X"));
assertEquals(" $ bash -c 'echo Standard Out; exit 1'", messageLines[1]);
assertEquals(" STDOUT: Standard Out", messageLines[2]);
}
}

Expand All @@ -273,9 +276,9 @@ public void testNonZeroResultYieldsExceptionWithStdErrAndStdout() {
assertTrue(ex.getExitValue() == 1);
assertTrue(ex.getTime() > 0);
final String[] messageLines = ex.getMessage().split("\n");
assertEquals("STDERR: Standard Error", messageLines[1].trim());
assertEquals("STDOUT: Standard Out Line 1", messageLines[2].trim());
assertEquals("STDOUT: Standard Out Line 2", messageLines[3].trim());
assertEquals(" STDERR: Standard Error", messageLines[2]);
assertEquals(" STDOUT: Standard Out Line 1", messageLines[3]);
assertEquals(" STDOUT: Standard Out Line 2", messageLines[4]);
}
}

Expand Down Expand Up @@ -539,9 +542,13 @@ public void testNonZeroResultYieldsExceptionAndDoesntFail() {
builder.run();
fail("Should throw exception");
} catch (ExternalProcessFailureException ex) {
assertTrue(ex.getMessage().endsWith("Stderr unavailable as it has been consumed by user provided stream."));
final String[] messageLines = ex.getMessage().split("\n");

assertEquals("External process `ls` terminated with unexpected exit status 1 after X:", messageLines[0].replaceAll("\\d+ms", "X"));
assertEquals(" $ ls xyz", messageLines[1]);
assertEquals(" STDERR: <Has already been consumed.>", messageLines[2]);
assertTrue(ex.getExitValue() > 0);
assertEquals("ls xyz", ex.getCommand());
assertEquals("ls xyz", ex.getCommandLine());
assertTrue(ex.getTime() > 0);

}
Expand Down

0 comments on commit 942e4be

Please sign in to comment.