Skip to content

Commit 1cd7667

Browse files
committed
Addition of a helper method
1 parent 662b478 commit 1cd7667

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

‎utils/src/main/java/com/cloud/utils/script/Script.java‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,20 @@ static String stackTraceAsString(Throwable throwable) {
244244
}
245245
}
246246

247+
/**
248+
* Builds the error result of a failed process whose output was drained by the interpreter task.
249+
* Draining interpreters (e.g. {@link OutputInterpreter.AllLinesParser}) return null from interpret(),
250+
* and callers treat a null result as success, so a non-null value must always be returned here:
251+
* the task result, otherwise the output captured by the parser, otherwise the process exit value.
252+
*/
253+
private String getDrainedErrorResult(Task task, OutputInterpreter interpreter) throws InterruptedException {
254+
String result = task.getResult();
255+
if (StringUtils.isBlank(result) && interpreter instanceof OutputInterpreter.AllLinesParser) {
256+
result = ((OutputInterpreter.AllLinesParser) interpreter).getLines();
257+
}
258+
return StringUtils.isNotBlank(result) ? result.trim() : String.valueOf(_process.exitValue());
259+
}
260+
247261
public String execute(OutputInterpreter interpreter) {
248262
return executeInternal(interpreter, null);
249263
}
@@ -371,7 +385,7 @@ private String executeInternal(OutputInterpreter interpreter, Map<String, String
371385

372386
String error;
373387
if (interpreter != null) {
374-
error = interpreter.drain() ? task.getResult() : interpreter.processError(reader);
388+
error = interpreter.drain() ? getDrainedErrorResult(task, interpreter) : interpreter.processError(reader);
375389
} else {
376390
error = String.valueOf(_process.exitValue());
377391
}
@@ -494,7 +508,7 @@ public String executeIgnoreExitValue(OutputInterpreter interpreter, int exitValu
494508

495509
String error;
496510
if (interpreter != null) {
497-
error = interpreter.drain() ? task.getResult() : interpreter.processError(reader);
511+
error = interpreter.drain() ? getDrainedErrorResult(task, interpreter) : interpreter.processError(reader);
498512
} else {
499513
error = String.valueOf(_process.exitValue());
500514
}

0 commit comments

Comments
 (0)