From 5ab8d98caafd71173d7106178163dc74f7dfa2fb Mon Sep 17 00:00:00 2001 From: Yury Brigadirenko Date: Wed, 18 Dec 2024 11:04:16 -0500 Subject: [PATCH] runtime-v2: allow throw payload with exception (#1049) --- .../concord/it/runtime/v2/ProcessIT.java | 18 ++++++++++++++++++ .../it/runtime/v2/throwWithPayload/concord.yml | 11 +++++++++++ .../plugins/throwex/ThrowExceptionTaskV2.java | 7 ++++--- .../v2/runner/vm/SaveLastErrorCommand.java | 6 +++++- .../runtime/v2/sdk/UserDefinedException.java | 16 ++++++++++++++-- 5 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 it/runtime-v2/src/test/resources/com/walmartlabs/concord/it/runtime/v2/throwWithPayload/concord.yml diff --git a/it/runtime-v2/src/test/java/com/walmartlabs/concord/it/runtime/v2/ProcessIT.java b/it/runtime-v2/src/test/java/com/walmartlabs/concord/it/runtime/v2/ProcessIT.java index f5fcf6e15d..4b6832222d 100644 --- a/it/runtime-v2/src/test/java/com/walmartlabs/concord/it/runtime/v2/ProcessIT.java +++ b/it/runtime-v2/src/test/java/com/walmartlabs/concord/it/runtime/v2/ProcessIT.java @@ -24,6 +24,7 @@ import ca.ibodrov.concord.testcontainers.Payload; import ca.ibodrov.concord.testcontainers.junit5.ConcordRule; import com.walmartlabs.concord.client2.*; +import com.walmartlabs.concord.common.ConfigurationUtils; import com.walmartlabs.concord.sdk.Constants; import com.walmartlabs.concord.sdk.MapUtils; import org.junit.jupiter.api.Test; @@ -195,6 +196,23 @@ public void testOutVariables() throws Exception { assertFalse(data.containsKey("z")); } + @Test + public void testThrowWithPayload() throws Exception { + Payload payload = new Payload() + .archive(resource("throwWithPayload")); + + ConcordProcess proc = concord.processes().start(payload); + expectStatus(proc, ProcessEntry.StatusEnum.FAILED); + + // --- + + Map data = proc.getOutVariables(); + assertNotNull(data); + + assertEquals("BOOM", ConfigurationUtils.get(data, "lastError", "message")); + assertEquals(Map.of("key", "value", "key2", "value2"), ConfigurationUtils.get(data, "lastError", "payload")); + } + @Test public void testLogsFromExpressions() throws Exception { Payload payload = new Payload() diff --git a/it/runtime-v2/src/test/resources/com/walmartlabs/concord/it/runtime/v2/throwWithPayload/concord.yml b/it/runtime-v2/src/test/resources/com/walmartlabs/concord/it/runtime/v2/throwWithPayload/concord.yml new file mode 100644 index 0000000000..605c6036ca --- /dev/null +++ b/it/runtime-v2/src/test/resources/com/walmartlabs/concord/it/runtime/v2/throwWithPayload/concord.yml @@ -0,0 +1,11 @@ +configuration: + runtime: "concord-v2" + +flows: + default: + - task: "throw" + in: + exception: "BOOM" + payload: + key: "value" + key2: "value2" diff --git a/plugins/tasks/throw/src/main/java/com/walmartlabs/concord/plugins/throwex/ThrowExceptionTaskV2.java b/plugins/tasks/throw/src/main/java/com/walmartlabs/concord/plugins/throwex/ThrowExceptionTaskV2.java index 6f22d44b0c..bdbf43e42f 100644 --- a/plugins/tasks/throw/src/main/java/com/walmartlabs/concord/plugins/throwex/ThrowExceptionTaskV2.java +++ b/plugins/tasks/throw/src/main/java/com/walmartlabs/concord/plugins/throwex/ThrowExceptionTaskV2.java @@ -24,6 +24,7 @@ import javax.inject.Named; import java.io.Serializable; +import java.util.Map; @Named("throw") @DryRunReady @@ -31,12 +32,12 @@ public class ThrowExceptionTaskV2 implements Task { @Override public TaskResult execute(Variables input) throws Exception { - Object exception = input.get("exception"); + var exception = input.get("exception"); if (exception instanceof Exception) { throw (Exception) exception; - } else if (exception instanceof String) { - throw new UserDefinedException(exception.toString()); + } else if (exception instanceof String s) { + throw new UserDefinedException(s, input.getMap("payload", Map.of())); } else if (exception instanceof Serializable) { throw new ConcordException("Process Error", (Serializable) exception); } else { diff --git a/runtime/v2/runner/src/main/java/com/walmartlabs/concord/runtime/v2/runner/vm/SaveLastErrorCommand.java b/runtime/v2/runner/src/main/java/com/walmartlabs/concord/runtime/v2/runner/vm/SaveLastErrorCommand.java index 1731968639..cd6ea3dd32 100644 --- a/runtime/v2/runner/src/main/java/com/walmartlabs/concord/runtime/v2/runner/vm/SaveLastErrorCommand.java +++ b/runtime/v2/runner/src/main/java/com/walmartlabs/concord/runtime/v2/runner/vm/SaveLastErrorCommand.java @@ -68,6 +68,10 @@ public void eval(Runtime runtime, State state, ThreadId threadId) throws Excepti private static Map serialize(Exception e) { try { + if (e instanceof LoggedException le) { + e = le.getCause(); + } + return createMapper().convertValue(e, MAP_TYPE); } catch (Exception ex) { // ignore ex @@ -83,7 +87,7 @@ private static ObjectMapper createMapper() { } @SuppressWarnings("unused") - @JsonInclude(JsonInclude.Include.NON_NULL) + @JsonInclude(JsonInclude.Include.NON_EMPTY) @JsonIdentityInfo(generator= ObjectIdGenerators.IntSequenceGenerator.class) abstract static class ExceptionMixIn { @JsonIgnore diff --git a/runtime/v2/sdk/src/main/java/com/walmartlabs/concord/runtime/v2/sdk/UserDefinedException.java b/runtime/v2/sdk/src/main/java/com/walmartlabs/concord/runtime/v2/sdk/UserDefinedException.java index 70bbe6ea7c..c9c370755a 100644 --- a/runtime/v2/sdk/src/main/java/com/walmartlabs/concord/runtime/v2/sdk/UserDefinedException.java +++ b/runtime/v2/sdk/src/main/java/com/walmartlabs/concord/runtime/v2/sdk/UserDefinedException.java @@ -9,9 +9,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -22,6 +22,7 @@ import java.io.PrintStream; import java.io.PrintWriter; +import java.util.Map; /** * Doesn't produce a stack trace in process logs. @@ -31,8 +32,15 @@ public class UserDefinedException extends RuntimeException { // for backward compatibility (java8 concord 1.92.0 version) private static final long serialVersionUID = 8152584338845805365L; + private final Map payload; + public UserDefinedException(String message) { + this(message, null); + } + + public UserDefinedException(String message, Map payload) { super(message); + this.payload = payload; } @Override @@ -44,4 +52,8 @@ public void printStackTrace(PrintStream s) { public void printStackTrace(PrintWriter s) { s.println(getMessage()); } + + public Map getPayload() { + return payload; + } }