Difference between Workflow.wrap() and ApplicationFailure.newFailure()

Hello, i was wondering, in a Activity which is the difference on doing Workflow.wrap(Exception e) and add to the RetryOptions associated to that activity “.setDoNotRetry(FullExceptionName)”
VS
calling, always from the Activity, the methods:

throw ApplicationFailure.newNonRetryableFailure(message, exceptionName)
OR
throw ApplicationFailure.newFailure(message, exceptionName)

Thanks

When any exception that doesn’t extend TemporalException or TemporalFailure is thrown from an activity body it is converted to an ApplicationFailure before returning it to a workflow. The full name of an exception class becomes ApplicationFailure.Type.

The only reason to use Activity.wrap (or Workflow.wrap in the workflow code) is to allow rethrowing checked exceptions as unchecked. The framework will ignore the wrapper exception and convert the original checked exception to the ApplicationFailure.

You can create an ApplicationFailure directly instead of relying on the framework default conversion. There are two main reasons to do this:

  1. ApplicationFailure.newFailure and ApplicationFailure.newNonRetryableFailure methods accept details field. The details field can contain any object that can be serialized/deserialized by a configured DataConverter. The details are available in the workflow through ApplicationFailure.getDetails.
  2. Use ApplicationFailure.newNonRetryableFailure to create a failure that is non retryable even if its type is not included into RetryOptions.DoNotRetry list.

Note that calls to activities always throw ActivityFailure with an exception that caused the failure as a cause. So a NullPointerException thrown from an activity is going to be delivered to the workflow (after exhausting all retries according to the retry options) as an ActivityFailure that has an ApplicationFailure with a type equal to `java.lang.NullPointerException" as a cause.

1 Like