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 | 102 +++++++++++----------- 1 file changed, 53 insertions(+), 49 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,54 +250,9 @@ public class BuildExecutor { break; } } catch (Throwable e) { - exitStatus(1); - - 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 (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 (first_exception) { - System.err.println(ExceptionUtils.getExceptionStackTrace(e)); - } - } - } + exitStatus(ExitStatusException.EXIT_FAILURE); + outputCommandExecutionException(e); + break; } } @@ -308,6 +263,55 @@ public class BuildExecutor { 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; + } + e2 = e2.getCause(); + } + + 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 (first_exception) { + System.err.println(ExceptionUtils.getExceptionStackTrace(e)); + } + } + } + } + /** * Starts the execution of the build. This method will call * System.exit() when done with the appropriate exit status. @@ -554,7 +558,7 @@ public class BuildExecutor { System.err.println(); System.err.println("ERROR: " + message); } - exitStatus(1); + exitStatus(ExitStatusException.EXIT_FAILURE); return false; } return true;