Best Practice for Explicitly failing workflow execution

Workflow execution stops when the @WorkflowMethod completes, so returning after a failed validation result from the activity is going to complete the workflow exec. Note that in that case the workflow status is “Completed”.

In some cases you want to fail the workflow and have it’s status set to “Failed”. Note that by default workflows don’t fail or “unknown” (intermittent) failures, meaning any exceptions that do no extend TemporalFailure
In those cases workflow blocks its execution and retries periodically. This is done in order to avoid exceptions such as NullPointerExceptions to fail workflow execution.

If you want to fail workflow on lets say MyValidationException (or any exception that does not extend TemporalFailure), you can include it in WorkflowImplementationOptions.setFailWorkflowExceptionTypes so for example:

Client code:

WorkflowImplementationOptions workflowImplementationOptions =
        WorkflowImplementationOptions.newBuilder()
            .setFailWorkflowExceptionTypes(MyValidationException.class)
            .build();
...

 worker.registerWorkflowImplementationTypes(
        workflowImplementationOptions, MyWorkflowImpl.class);

Workflow code:

throw Workflow.wrap(new MyValidationException("Validation failed"));`
2 Likes