Skip to content

Commit e3ef9b4

Browse files
Standardized update failure exception (#2339)
1 parent 5212a34 commit e3ef9b4

File tree

8 files changed

+49
-16
lines changed

8 files changed

+49
-16
lines changed

temporal-sdk/src/main/java/io/temporal/client/WorkflowStub.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,11 @@ static <T> WorkflowStub fromTyped(T typed) {
8383
* @param <R> type of the update return value
8484
* @param args update method arguments
8585
* @return update result
86-
* @throws WorkflowNotFoundException if the workflow execution doesn't exist or completed and
87-
* can't be signalled
86+
* @throws WorkflowUpdateException if the update is rejected or failed during it's execution by
87+
* the workflow.
88+
* @throws WorkflowNotFoundException if the workflow execution doesn't exist or is completed.
8889
* @throws WorkflowServiceException for all other failures including networking and service
89-
* availability issues
90+
* availability issues.
9091
*/
9192
@Experimental
9293
<R> R update(String updateName, Class<R> resultClass, Object... args);
@@ -103,6 +104,9 @@ static <T> WorkflowStub fromTyped(T typed) {
103104
* @param <R> type of the update return value
104105
* @param args update method arguments
105106
* @return update handle that can be used to get the result of the update.
107+
* @throws WorkflowNotFoundException if the workflow execution doesn't exist or completed.
108+
* @throws WorkflowServiceException for all other failures including networking and service
109+
* availability issues.
106110
*/
107111
@Experimental
108112
<R> WorkflowUpdateHandle<R> startUpdate(
@@ -116,6 +120,9 @@ <R> WorkflowUpdateHandle<R> startUpdate(
116120
* @param options options that will be used to configure and start a new update request.
117121
* @param args update method arguments
118122
* @return update handle that can be used to get the result of the update.
123+
* @throws WorkflowNotFoundException if the workflow execution doesn't exist or completed.
124+
* @throws WorkflowServiceException for all other failures including networking and service
125+
* availability issues.
119126
*/
120127
@Experimental
121128
<R> WorkflowUpdateHandle<R> startUpdate(UpdateOptions<R> options, Object... args);

temporal-sdk/src/main/java/io/temporal/client/WorkflowUpdateHandle.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public interface WorkflowUpdateHandle<T> {
4949
* Returns the result of the workflow update.
5050
*
5151
* @return the result of the workflow update
52+
* @throws WorkflowUpdateException if the update was rejected or failed by the workflow.
5253
*/
5354
T getResult();
5455

@@ -58,6 +59,7 @@ public interface WorkflowUpdateHandle<T> {
5859
* @param timeout maximum time to wait and perform the background long polling
5960
* @param unit unit of timeout
6061
* @throws WorkflowUpdateTimeoutOrCancelledException if the timeout is reached.
62+
* @throws WorkflowUpdateException if the update was rejected or failed by the workflow.
6163
* @return the result of the workflow update
6264
*/
6365
T getResult(long timeout, TimeUnit unit);

temporal-sdk/src/main/java/io/temporal/internal/client/CompletedWorkflowUpdateHandleImpl.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package io.temporal.internal.client;
2222

2323
import io.temporal.api.common.v1.WorkflowExecution;
24+
import io.temporal.client.WorkflowUpdateException;
2425
import io.temporal.client.WorkflowUpdateHandle;
2526
import io.temporal.common.Experimental;
2627
import java.util.concurrent.CompletableFuture;
@@ -31,12 +32,22 @@ public final class CompletedWorkflowUpdateHandleImpl<T> implements WorkflowUpdat
3132

3233
private final String id;
3334
private final WorkflowExecution execution;
35+
private final WorkflowUpdateException exception;
3436
private final T result;
3537

3638
public CompletedWorkflowUpdateHandleImpl(String id, WorkflowExecution execution, T result) {
3739
this.id = id;
3840
this.execution = execution;
3941
this.result = result;
42+
this.exception = null;
43+
}
44+
45+
public CompletedWorkflowUpdateHandleImpl(
46+
String id, WorkflowExecution execution, WorkflowUpdateException ex) {
47+
this.id = id;
48+
this.execution = execution;
49+
this.exception = ex;
50+
this.result = null;
4051
}
4152

4253
@Override
@@ -51,21 +62,29 @@ public String getId() {
5162

5263
@Override
5364
public T getResult() {
65+
if (exception != null) {
66+
throw exception;
67+
}
5468
return result;
5569
}
5670

5771
@Override
5872
public T getResult(long timeout, TimeUnit unit) {
59-
return result;
73+
return getResult();
6074
}
6175

6276
@Override
6377
public CompletableFuture<T> getResultAsync() {
78+
if (exception != null) {
79+
CompletableFuture<T> result = new CompletableFuture<>();
80+
result.completeExceptionally(exception);
81+
return result;
82+
}
6483
return CompletableFuture.completedFuture(result);
6584
}
6685

6786
@Override
6887
public CompletableFuture<T> getResultAsync(long timeout, TimeUnit unit) {
69-
return CompletableFuture.completedFuture(result);
88+
return getResultAsync();
7089
}
7190
}

temporal-sdk/src/main/java/io/temporal/internal/client/RootWorkflowClientInvoker.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -533,12 +533,15 @@ private <R> WorkflowUpdateHandle<R> toUpdateHandle(
533533
result.getUpdateRef().getWorkflowExecution(),
534534
resultValue);
535535
case FAILURE:
536-
throw new WorkflowUpdateException(
537-
result.getUpdateRef().getWorkflowExecution(),
536+
return new CompletedWorkflowUpdateHandleImpl<>(
538537
result.getUpdateRef().getUpdateId(),
539-
input.getUpdateName(),
540-
dataConverterWithWorkflowContext.failureToException(
541-
result.getOutcome().getFailure()));
538+
result.getUpdateRef().getWorkflowExecution(),
539+
new WorkflowUpdateException(
540+
result.getUpdateRef().getWorkflowExecution(),
541+
result.getUpdateRef().getUpdateId(),
542+
input.getUpdateName(),
543+
dataConverterWithWorkflowContext.failureToException(
544+
result.getOutcome().getFailure())));
542545
default:
543546
throw new RuntimeException(
544547
"Received unexpected outcome from update request: "

temporal-sdk/src/test/java/io/temporal/workflow/updateTest/DynamicUpdateTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void dynamicUpdate() throws ExecutionException, InterruptedException {
7272
WorkflowUpdateException.class,
7373
() ->
7474
stub.startUpdate("reject", WorkflowUpdateStage.COMPLETED, String.class, "update input")
75-
.getResultAsync());
75+
.getResult());
7676

7777
stub.startUpdate("complete", WorkflowUpdateStage.COMPLETED, Void.class).getResultAsync().get();
7878

temporal-sdk/src/test/java/io/temporal/workflow/updateTest/UpdateInfoTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ public void testUpdateInfo() throws ExecutionException, InterruptedException {
7272
WorkflowUpdateException.class,
7373
() ->
7474
stub.startUpdate(updateOptionsBuilder.setUpdateId("reject").build(), 0, "")
75-
.getResultAsync());
76-
75+
.getResult());
7776
workflow.complete();
7877
String result =
7978
testWorkflowRule

temporal-sdk/src/test/java/io/temporal/workflow/updateTest/UpdateTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,10 @@ public void testUpdateUntyped() throws ExecutionException, InterruptedException
199199
// send a bad update that will be rejected through the sync path
200200
assertThrows(
201201
WorkflowUpdateException.class,
202-
() -> workflowStub.startUpdate("update", ACCEPTED, String.class, 0, "Bad Update"));
202+
() ->
203+
workflowStub
204+
.startUpdate("update", ACCEPTED, String.class, 0, "Bad Update")
205+
.getResult());
203206

204207
workflowStub.update("complete", void.class);
205208

temporal-sdk/src/test/java/io/temporal/workflow/updateTest/UpdateWithStartTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,8 @@ public void failWhenUpdatedIsRejected() {
478478
.build();
479479

480480
assertThrows(
481-
WorkflowServiceException.class,
482-
() -> WorkflowClient.updateWithStart(workflow::execute, updateOp));
481+
WorkflowUpdateException.class,
482+
() -> WorkflowClient.updateWithStart(workflow::execute, updateOp).getResult());
483483
}
484484

485485
@Test

0 commit comments

Comments
 (0)