From 2e6da34b46f5434893685d9246f0df765d9f0a1f Mon Sep 17 00:00:00 2001 From: Mathieu Cartoixa Date: Wed, 24 Feb 2021 14:34:30 +0100 Subject: [PATCH] Handle unexpected errors --- .../com/mcartoixa/ant/sfdx/ErrorOutputStream.java | 14 ++++++++++++-- .../com/mcartoixa/ant/sfdx/ISfdxOutputHandler.java | 3 ++- src/main/com/mcartoixa/ant/sfdx/SfdxTask.java | 7 +++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main/com/mcartoixa/ant/sfdx/ErrorOutputStream.java b/src/main/com/mcartoixa/ant/sfdx/ErrorOutputStream.java index d54c3e1..f922306 100644 --- a/src/main/com/mcartoixa/ant/sfdx/ErrorOutputStream.java +++ b/src/main/com/mcartoixa/ant/sfdx/ErrorOutputStream.java @@ -15,6 +15,7 @@ */ package com.mcartoixa.ant.sfdx; +import java.io.IOException; import org.apache.tools.ant.Project; import org.apache.tools.ant.util.LineOrientedOutputStream; @@ -32,16 +33,25 @@ public class ErrorOutputStream extends LineOrientedOutputStream { public ErrorOutputStream(final ISfdxOutputHandler handler) { super(); this.handler = handler; + this.errorMessage = ""; } @Override protected void processLine(final String string) { if (string != null && !string.isEmpty()) { - // Proper messages will be generated from the JSON response (if there is one!) handler.log(string, Project.MSG_VERBOSE); + this.errorMessage = this.errorMessage.concat(string + "\n"); } } - private final transient ISfdxOutputHandler handler; + @Override + public void close() throws IOException { + if (!this.errorMessage.isEmpty()) { + handler.setErrorMessage(this.errorMessage); + } + super.close(); + } + private final transient ISfdxOutputHandler handler; + private transient String errorMessage; } diff --git a/src/main/com/mcartoixa/ant/sfdx/ISfdxOutputHandler.java b/src/main/com/mcartoixa/ant/sfdx/ISfdxOutputHandler.java index 4549adf..f6fde1d 100644 --- a/src/main/com/mcartoixa/ant/sfdx/ISfdxOutputHandler.java +++ b/src/main/com/mcartoixa/ant/sfdx/ISfdxOutputHandler.java @@ -22,5 +22,6 @@ public interface ISfdxOutputHandler { void log(String message, int level); - + + void setErrorMessage(String message); } diff --git a/src/main/com/mcartoixa/ant/sfdx/SfdxTask.java b/src/main/com/mcartoixa/ant/sfdx/SfdxTask.java index 5f496b3..139dfc4 100644 --- a/src/main/com/mcartoixa/ant/sfdx/SfdxTask.java +++ b/src/main/com/mcartoixa/ant/sfdx/SfdxTask.java @@ -63,6 +63,13 @@ public void parse(final JSONObject json) { } } + @Override + public void setErrorMessage(final String message) { + if (SfdxTask.this.getFailOnError() && !SfdxTask.this.hasErrorMessage()) { + SfdxTask.this.setErrorMessage(message.trim()); + } + } + @SuppressWarnings({"PMD.DataflowAnomalyAnalysis", "PMD.NPathComplexity"}) protected void doParse(final JSONObject json) { final int status = json.optInt("status");