Best Practice for Explicitly failing workflow execution

Hi,

I have a workflow where I have a “Validate” activity. The validate activity will connect to an external system and check to see if the workflow can continue, data provided is correct, resources are not already in use etc. The activity can return a boolean success flag and an additional message. If all validations are successful, success flag = true, otherwise, false and message contains further information.

Question I have is, what is the recommended way to deal with validation failures? - I want the workflow execution to stop as validation has failed.

Should the Activity throw an “ApplicationFailure” Exception? Should the activity just return the result to workflow, and workflow checks status of the flag and then throw an Exception? Workflow to return?

Would be interested in hearing if there are any benefits/pitfalls about the various options.

Thanks,
Jay.

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"));`
1 Like