Skip to content

Commit 5ab8d98

Browse files
authored
runtime-v2: allow throw payload with exception (#1049)
1 parent b44f1cf commit 5ab8d98

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

it/runtime-v2/src/test/java/com/walmartlabs/concord/it/runtime/v2/ProcessIT.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import ca.ibodrov.concord.testcontainers.Payload;
2525
import ca.ibodrov.concord.testcontainers.junit5.ConcordRule;
2626
import com.walmartlabs.concord.client2.*;
27+
import com.walmartlabs.concord.common.ConfigurationUtils;
2728
import com.walmartlabs.concord.sdk.Constants;
2829
import com.walmartlabs.concord.sdk.MapUtils;
2930
import org.junit.jupiter.api.Test;
@@ -195,6 +196,23 @@ public void testOutVariables() throws Exception {
195196
assertFalse(data.containsKey("z"));
196197
}
197198

199+
@Test
200+
public void testThrowWithPayload() throws Exception {
201+
Payload payload = new Payload()
202+
.archive(resource("throwWithPayload"));
203+
204+
ConcordProcess proc = concord.processes().start(payload);
205+
expectStatus(proc, ProcessEntry.StatusEnum.FAILED);
206+
207+
// ---
208+
209+
Map<String, Object> data = proc.getOutVariables();
210+
assertNotNull(data);
211+
212+
assertEquals("BOOM", ConfigurationUtils.get(data, "lastError", "message"));
213+
assertEquals(Map.of("key", "value", "key2", "value2"), ConfigurationUtils.get(data, "lastError", "payload"));
214+
}
215+
198216
@Test
199217
public void testLogsFromExpressions() throws Exception {
200218
Payload payload = new Payload()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
configuration:
2+
runtime: "concord-v2"
3+
4+
flows:
5+
default:
6+
- task: "throw"
7+
in:
8+
exception: "BOOM"
9+
payload:
10+
key: "value"
11+
key2: "value2"

plugins/tasks/throw/src/main/java/com/walmartlabs/concord/plugins/throwex/ThrowExceptionTaskV2.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,20 @@
2424

2525
import javax.inject.Named;
2626
import java.io.Serializable;
27+
import java.util.Map;
2728

2829
@Named("throw")
2930
@DryRunReady
3031
public class ThrowExceptionTaskV2 implements Task {
3132

3233
@Override
3334
public TaskResult execute(Variables input) throws Exception {
34-
Object exception = input.get("exception");
35+
var exception = input.get("exception");
3536

3637
if (exception instanceof Exception) {
3738
throw (Exception) exception;
38-
} else if (exception instanceof String) {
39-
throw new UserDefinedException(exception.toString());
39+
} else if (exception instanceof String s) {
40+
throw new UserDefinedException(s, input.getMap("payload", Map.of()));
4041
} else if (exception instanceof Serializable) {
4142
throw new ConcordException("Process Error", (Serializable) exception);
4243
} else {

runtime/v2/runner/src/main/java/com/walmartlabs/concord/runtime/v2/runner/vm/SaveLastErrorCommand.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ public void eval(Runtime runtime, State state, ThreadId threadId) throws Excepti
6868

6969
private static Map<String, Object> serialize(Exception e) {
7070
try {
71+
if (e instanceof LoggedException le) {
72+
e = le.getCause();
73+
}
74+
7175
return createMapper().convertValue(e, MAP_TYPE);
7276
} catch (Exception ex) {
7377
// ignore ex
@@ -83,7 +87,7 @@ private static ObjectMapper createMapper() {
8387
}
8488

8589
@SuppressWarnings("unused")
86-
@JsonInclude(JsonInclude.Include.NON_NULL)
90+
@JsonInclude(JsonInclude.Include.NON_EMPTY)
8791
@JsonIdentityInfo(generator= ObjectIdGenerators.IntSequenceGenerator.class)
8892
abstract static class ExceptionMixIn {
8993
@JsonIgnore

runtime/v2/sdk/src/main/java/com/walmartlabs/concord/runtime/v2/sdk/UserDefinedException.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* Licensed under the Apache License, Version 2.0 (the "License");
1010
* you may not use this file except in compliance with the License.
1111
* You may obtain a copy of the License at
12-
*
12+
*
1313
* http://www.apache.org/licenses/LICENSE-2.0
14-
*
14+
*
1515
* Unless required by applicable law or agreed to in writing, software
1616
* distributed under the License is distributed on an "AS IS" BASIS,
1717
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -22,6 +22,7 @@
2222

2323
import java.io.PrintStream;
2424
import java.io.PrintWriter;
25+
import java.util.Map;
2526

2627
/**
2728
* Doesn't produce a stack trace in process logs.
@@ -31,8 +32,15 @@ public class UserDefinedException extends RuntimeException {
3132
// for backward compatibility (java8 concord 1.92.0 version)
3233
private static final long serialVersionUID = 8152584338845805365L;
3334

35+
private final Map<String, Object> payload;
36+
3437
public UserDefinedException(String message) {
38+
this(message, null);
39+
}
40+
41+
public UserDefinedException(String message, Map<String, Object> payload) {
3542
super(message);
43+
this.payload = payload;
3644
}
3745

3846
@Override
@@ -44,4 +52,8 @@ public void printStackTrace(PrintStream s) {
4452
public void printStackTrace(PrintWriter s) {
4553
s.println(getMessage());
4654
}
55+
56+
public Map<String, Object> getPayload() {
57+
return payload;
58+
}
4759
}

0 commit comments

Comments
 (0)