Skip to content

Commit dd6c143

Browse files
committed
[Fix #1198] Adding error variable
Signed-off-by: fjtirado <ftirados@redhat.com>
1 parent 391efc7 commit dd6c143

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

impl/core/src/main/java/io/serverlessworkflow/impl/executors/TryExecutor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class TryExecutor extends RegularTaskExecutor<TryTask> {
5252
private final TaskExecutor<?> taskExecutor;
5353
private final Optional<TaskExecutor<?>> catchTaskExecutor;
5454
private final Optional<RetryExecutor> retryIntervalExecutor;
55+
private final String errorVariable;
5556

5657
public static class TryExecutorBuilder extends RegularTaskExecutorBuilder<TryTask> {
5758

@@ -61,6 +62,7 @@ public static class TryExecutorBuilder extends RegularTaskExecutorBuilder<TryTas
6162
private final TaskExecutor<?> taskExecutor;
6263
private final Optional<TaskExecutor<?>> catchTaskExecutor;
6364
private final Optional<RetryExecutor> retryIntervalExecutor;
65+
private String errorVariable;
6466

6567
protected TryExecutorBuilder(
6668
WorkflowMutablePosition position, TryTask task, WorkflowDefinition definition) {
@@ -73,8 +75,8 @@ protected TryExecutorBuilder(
7375
TaskExecutorHelper.createExecutorList(position, task.getTry(), definition);
7476
TryTaskCatch catchTask = task.getCatch();
7577
if (catchTask != null) {
78+
this.errorVariable = catchTask.getAs();
7679
List<TaskItem> catchTaskDo = catchTask.getDo();
77-
7880
this.catchTaskExecutor =
7981
catchTaskDo != null && !catchTaskDo.isEmpty()
8082
? Optional.of(
@@ -144,6 +146,7 @@ protected TryExecutor(TryExecutorBuilder builder) {
144146
this.taskExecutor = builder.taskExecutor;
145147
this.catchTaskExecutor = builder.catchTaskExecutor;
146148
this.retryIntervalExecutor = builder.retryIntervalExecutor;
149+
this.errorVariable = builder.errorVariable;
147150
}
148151

149152
@Override
@@ -171,6 +174,9 @@ private CompletableFuture<WorkflowModel> handleException(
171174
if (errorFilter.map(f -> f.test(exception.getWorkflowError())).orElse(true)
172175
&& WorkflowUtils.whenExceptTest(
173176
whenFilter, exceptFilter, workflow, taskContext, taskContext.rawOutput())) {
177+
if (errorVariable != null) {
178+
taskContext.variables().put(errorVariable, exception.getWorkflowError());
179+
}
174180
if (catchTaskExecutor.isPresent()) {
175181
completable =
176182
completable.thenCompose(

impl/test/src/test/java/io/serverlessworkflow/impl/test/RetryTimeoutTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,18 @@ void testDoesNotMatch(String path) {
137137
.join())
138138
.hasCauseInstanceOf(WorkflowException.class);
139139
}
140+
141+
@Test
142+
void testErrorVariable() throws IOException {
143+
assertThat(
144+
app.workflowDefinition(
145+
readWorkflowFromClasspath("workflows-samples/try-catch-error-variable.yaml"))
146+
.instance(Map.of())
147+
.start()
148+
.join()
149+
.asMap()
150+
.map(m -> m.get("errorMessage"))
151+
.orElseThrow())
152+
.isEqualTo("Javierito was here!");
153+
}
140154
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
document:
2+
dsl: '1.0.0'
3+
namespace: test
4+
name: try-catch-error-variable
5+
version: '0.1.0'
6+
do:
7+
- attemptTask:
8+
try:
9+
- failingTask:
10+
raise:
11+
error:
12+
type: https://example.com/errors/transient
13+
detail: Javierito was here!
14+
status: 503
15+
catch:
16+
as: caughtError
17+
do:
18+
- handleError:
19+
set:
20+
errorMessage: ${$caughtError.details}

0 commit comments

Comments
 (0)