Skip to content

Commit

Permalink
Add conversion for common errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Quinn-With-Two-Ns committed Sep 25, 2024
1 parent 8d7b3cd commit 860f6d1
Showing 1 changed file with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import io.temporal.api.common.v1.Payload;
import io.temporal.api.nexus.v1.*;
import io.temporal.client.WorkflowClient;
import io.temporal.client.WorkflowException;
import io.temporal.common.converter.DataConverter;
import io.temporal.failure.ApplicationFailure;
import io.temporal.internal.common.NexusUtil;
import io.temporal.internal.worker.NexusTask;
import io.temporal.internal.worker.NexusTaskHandler;
Expand Down Expand Up @@ -177,12 +179,34 @@ private CancelOperationResponse handleCancelledOperation(

OperationCancelDetails operationCancelDetails =
OperationCancelDetails.newBuilder().setOperationId(task.getOperationId()).build();

serviceHandler.cancelOperation(ctx.build(), operationCancelDetails);
try {
serviceHandler.cancelOperation(ctx.build(), operationCancelDetails);
} catch (Throwable failure) {
convertKnownFailures(failure);
}

return CancelOperationResponse.newBuilder().build();
}

private void convertKnownFailures(Throwable failure) {
if (failure instanceof ApplicationFailure) {
if (((ApplicationFailure) failure).isNonRetryable()) {
throw new OperationHandlerException(
OperationHandlerException.ErrorType.BAD_REQUEST, failure.getMessage());
}
throw new OperationHandlerException(
OperationHandlerException.ErrorType.INTERNAL, failure.getMessage());
}
if (failure instanceof WorkflowException) {
throw new OperationHandlerException(
OperationHandlerException.ErrorType.BAD_REQUEST, failure.getMessage());
}
if (failure instanceof Error) {
throw (Error) failure;
}
throw new RuntimeException(failure);
}

private StartOperationResponse handleStartOperation(
OperationContext.Builder ctx, StartOperationRequest task)
throws InvalidProtocolBufferException {
Expand Down Expand Up @@ -222,6 +246,8 @@ private StartOperationResponse handleStartOperation(
.putAllMetadata(e.getFailureInfo().getMetadata())
.build())
.build());
} catch (Throwable failure) {
convertKnownFailures(failure);
}
return startResponseBuilder.build();
}
Expand Down

0 comments on commit 860f6d1

Please sign in to comment.