This is an automated email from the ASF dual-hosted git repository.

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git


The following commit(s) were added to refs/heads/master by this push:
     new 786572257c allow fail to include a value
786572257c is described below

commit 786572257c62b59bf180b33ba80d3c8f655aae35
Author: Alex Heneveld <[email protected]>
AuthorDate: Thu Aug 17 17:29:08 2023 +0100

    allow fail to include a value
    
    useful when workflows fail but there is context or a result which might be 
of interest
---
 .../WorkflowStepInstanceExecutionContext.java      |  6 +++++
 .../core/workflow/steps/flow/FailWorkflowStep.java | 26 ++++++++++++++++++++--
 2 files changed, 30 insertions(+), 2 deletions(-)

diff --git 
a/core/src/main/java/org/apache/brooklyn/core/workflow/WorkflowStepInstanceExecutionContext.java
 
b/core/src/main/java/org/apache/brooklyn/core/workflow/WorkflowStepInstanceExecutionContext.java
index b6c6dee997..704904f692 100644
--- 
a/core/src/main/java/org/apache/brooklyn/core/workflow/WorkflowStepInstanceExecutionContext.java
+++ 
b/core/src/main/java/org/apache/brooklyn/core/workflow/WorkflowStepInstanceExecutionContext.java
@@ -158,6 +158,12 @@ public class WorkflowStepInstanceExecutionContext {
     public <T> T getInput(String key, TypeToken<T> type) {
         return 
getInput(WorkflowExpressionResolution.WorkflowExpressionStage.STEP_INPUT, key, 
type);
     }
+    public boolean hasInput(ConfigKey<?> key) {
+        return hasInput(key.getName());
+    }
+    public boolean hasInput(String key) {
+        return input.containsKey(key);
+    }
     public <T> T getInput(WorkflowExpressionResolution.WorkflowExpressionStage 
stage, String key, TypeToken<T> type) {
         if (inputResolved.containsKey(key)) return (T)inputResolved.get(key);
 
diff --git 
a/core/src/main/java/org/apache/brooklyn/core/workflow/steps/flow/FailWorkflowStep.java
 
b/core/src/main/java/org/apache/brooklyn/core/workflow/steps/flow/FailWorkflowStep.java
index 4018895019..906b624a54 100644
--- 
a/core/src/main/java/org/apache/brooklyn/core/workflow/steps/flow/FailWorkflowStep.java
+++ 
b/core/src/main/java/org/apache/brooklyn/core/workflow/steps/flow/FailWorkflowStep.java
@@ -22,6 +22,7 @@ import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.config.ConfigKeys;
 import org.apache.brooklyn.core.workflow.WorkflowStepDefinition;
 import org.apache.brooklyn.core.workflow.WorkflowStepInstanceExecutionContext;
+import org.apache.brooklyn.util.exceptions.Exceptions;
 import org.apache.brooklyn.util.text.Strings;
 
 public class FailWorkflowStep extends WorkflowStepDefinition {
@@ -30,6 +31,7 @@ public class FailWorkflowStep extends WorkflowStepDefinition {
 
     public static final ConfigKey<Boolean> RETHROW = 
ConfigKeys.newBooleanConfigKey("rethrow");
     public static final ConfigKey<String> MESSAGE = 
ConfigKeys.newStringConfigKey("message");
+    public static final ConfigKey<Object> VALUE = 
ConfigKeys.newConfigKey(Object.class, "value");
 
     @Override
     public void populateFromShorthand(String expression) {
@@ -40,7 +42,7 @@ public class FailWorkflowStep extends WorkflowStepDefinition {
     protected Object doTaskBody(WorkflowStepInstanceExecutionContext context) {
         Boolean rethrow = context.getInput(RETHROW);
         String message = context.getInput(MESSAGE);
-        boolean hasError = context.getError() != null;
+        Object value = context.getInput(VALUE);
         Throwable cause = context.getError();
         if (cause==null && Boolean.TRUE.equals(rethrow)) cause = new 
IllegalArgumentException("Fail specified with rethrow but no contextual error 
available");
         if (Boolean.FALSE.equals(rethrow)) cause = null;
@@ -51,12 +53,17 @@ public class FailWorkflowStep extends 
WorkflowStepDefinition {
         if (Strings.isBlank(message) && cause instanceof Error) {
             throw (Error) cause;
         }
+        if (value==null && cause!=null && !context.hasInput(VALUE)) {
+            value = WorkflowFailException.getValueFromCausalChain(cause);
+        }
 
-        throw new WorkflowFailException(message, cause);
+        throw new WorkflowFailException(message, cause, value);
     }
 
 
     public static class WorkflowFailException extends RuntimeException {
+        Object value;
+
         public WorkflowFailException() {
         }
 
@@ -68,9 +75,24 @@ public class FailWorkflowStep extends WorkflowStepDefinition 
{
             super(message, cause);
         }
 
+        public WorkflowFailException(String message, Throwable cause, Object 
value) {
+            super(message, cause);
+            this.value = value;
+        }
+
         public WorkflowFailException(Throwable cause) {
             super(cause);
         }
+
+        public Object getValue() {
+            return value;
+        }
+
+        public static Object getValueFromCausalChain(Throwable cause) {
+            WorkflowFailException wfeInCausalChain = 
Exceptions.getFirstThrowableOfType(cause, WorkflowFailException.class);
+            if (wfeInCausalChain==null) return null;
+            return wfeInCausalChain.getValue();
+        }
     }
 
     @Override protected Boolean isDefaultIdempotent() { return false; }

Reply via email to