From b9ac76b5b5ef12686632d38f4d13a6e5e7963dbd Mon Sep 17 00:00:00 2001 From: Geert Bevin Date: Wed, 26 Jun 2024 08:02:02 -0400 Subject: [PATCH] Interrupt execution when exception triggers during command execution. Minor refactoring. --- src/main/java/rife/bld/BuildExecutor.java | 98 ++++++++++++----------- 1 file changed, 51 insertions(+), 47 deletions(-) diff --git a/src/main/java/rife/bld/BuildExecutor.java b/src/main/java/rife/bld/BuildExecutor.java index 60e93f6..149208e 100644 --- a/src/main/java/rife/bld/BuildExecutor.java +++ b/src/main/java/rife/bld/BuildExecutor.java @@ -250,62 +250,66 @@ public int execute(String[] arguments) { break; } } catch (Throwable e) { - exitStatus(1); + exitStatus(ExitStatusException.EXIT_FAILURE); + outputCommandExecutionException(e); + break; + } + } - if (outputJson()) { - var t = TemplateFactory.JSON.get("bld.executor_error"); - if (showStacktrace) { - t.setValueEncoded("error-message", ExceptionUtils.getExceptionStackTrace(e)); - } - else { - boolean first_exception = true; - var e2 = e; - while (e2 != null) { - if (e2.getMessage() != null) { - t.setValueEncoded("error-message", e2.getMessage()); - first_exception = false; - } - e2 = e2.getCause(); - } + if (outputJson() && exitStatus_ == ExitStatusException.EXIT_SUCCESS) { + System.out.println(json_template.getContent()); + } - if (first_exception) { - t.setValueEncoded("error-message", ExceptionUtils.getExceptionStackTrace(e)); - } + return exitStatus_; + } + + private void outputCommandExecutionException(Throwable e) { + if (outputJson()) { + var t = TemplateFactory.JSON.get("bld.executor_error"); + if (showStacktrace) { + t.setValueEncoded("error-message", ExceptionUtils.getExceptionStackTrace(e)); + } + else { + boolean first_exception = true; + var e2 = e; + while (e2 != null) { + if (e2.getMessage() != null) { + t.setValueEncoded("error-message", e2.getMessage()); + first_exception = false; } - System.out.println(t.getContent()); + e2 = e2.getCause(); } - else { - System.err.println(); - - if (showStacktrace) { - System.err.println(ExceptionUtils.getExceptionStackTrace(e)); - } else { - boolean first_exception = true; - var e2 = e; - while (e2 != null) { - if (e2.getMessage() != null) { - if (!first_exception) { - System.err.print("> "); - } - System.err.println(e2.getMessage()); - first_exception = false; - } - e2 = e2.getCause(); - } - if (first_exception) { - System.err.println(ExceptionUtils.getExceptionStackTrace(e)); - } - } + if (first_exception) { + t.setValueEncoded("error-message", ExceptionUtils.getExceptionStackTrace(e)); } } + System.out.println(t.getContent()); } + else { + System.err.println(); + + if (showStacktrace) { + System.err.println(ExceptionUtils.getExceptionStackTrace(e)); + } else { + boolean first_exception = true; + var e2 = e; + while (e2 != null) { + if (e2.getMessage() != null) { + if (!first_exception) { + System.err.print("> "); + } + System.err.println(e2.getMessage()); + first_exception = false; + } + e2 = e2.getCause(); + } - if (outputJson() && exitStatus_ == ExitStatusException.EXIT_SUCCESS) { - System.out.println(json_template.getContent()); + if (first_exception) { + System.err.println(ExceptionUtils.getExceptionStackTrace(e)); + } + } } - - return exitStatus_; } /** @@ -554,7 +558,7 @@ private boolean executeCommand(Template jsonTemplate, String command) System.err.println(); System.err.println("ERROR: " + message); } - exitStatus(1); + exitStatus(ExitStatusException.EXIT_FAILURE); return false; } return true;