Skip to content

Commit

Permalink
Merge branch 'master' into ib/misc-security
Browse files Browse the repository at this point in the history
  • Loading branch information
ibodrov authored Dec 18, 2024
2 parents 60725e1 + 5ab8d98 commit 481bb06
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Object> 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()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
configuration:
runtime: "concord-v2"

flows:
default:
- task: "throw"
in:
exception: "BOOM"
payload:
key: "value"
key2: "value2"
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,20 @@

import javax.inject.Named;
import java.io.Serializable;
import java.util.Map;

@Named("throw")
@DryRunReady
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public void eval(Runtime runtime, State state, ThreadId threadId) throws Excepti

private static Map<String, Object> serialize(Exception e) {
try {
if (e instanceof LoggedException le) {
e = le.getCause();
}

return createMapper().convertValue(e, MAP_TYPE);
} catch (Exception ex) {
// ignore ex
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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<String, Object> payload;

public UserDefinedException(String message) {
this(message, null);
}

public UserDefinedException(String message, Map<String, Object> payload) {
super(message);
this.payload = payload;
}

@Override
Expand All @@ -44,4 +52,8 @@ public void printStackTrace(PrintStream s) {
public void printStackTrace(PrintWriter s) {
s.println(getMessage());
}

public Map<String, Object> getPayload() {
return payload;
}
}

0 comments on commit 481bb06

Please sign in to comment.