Activity exception is wrapped as ApplicationFailure in the workflow

Hi! We have a simple workflow with an activity

public class MyException extends RuntimeException {
  private Integer data; 
  public MyException(Integer data) { this.data = data; }
  public Integer getData() { return data; }
}

public class MyActivityImpl implements MyActivity {
  @Override
  public void doSomething() {
    if (someSpecificCondition) throw new MyException(1234);
    ...
  }
}

That is called from the workflow

@WorkflowInterface
public interface MyWorkflow {
    @WorkflowMethod
    void myMethod();
}

public class MyWorkflowImpl implements MyWorkflow {

  private final MyActivity myActivity;
  ...
  @Override
  public void myMethod() {
    try {
      myActivity.doSomething();
    }
    catch (TemporalException e) {
      // Handle exception when retries are exhausted
      // using ((MyException)...).getData()
    }
  }
}

And after reading this other thread ( Handling Exceptions from Activities with a RetryPolicy ) we are correctly navigating through the causes to find the underlying exceptions.

We are now needing to retrieve the information passed to the exception, but at the catch every exception in the stack is an ApplicationFailure containing the exception type and the message, but not the data inside the exception. So, we cannot call getData to retrieve the specific information.

Is there any way to propagate the custom exception, or include it as part of the ApplicationFailure? Should we completely reconsider that approach?

Thank you,
Pablo.

In your Activity you should not throw checked exceptions. If you need to propagate a checked exception, use Activity.wrap, for example:

  if (someSpecificCondition) {
    throw Activity.wrap(new MyException("This is my custom activity exception message"));
 }

Then in your workflow you can get this message out with for example:

try {
   return activities. doSomething();
} catch (ActivityFailure e) {
   logger.error(((ApplicationFailure) e.getCause()).getOriginalMessage());
   ...
}
1 Like

Thanks for the answer,

The exception we are throwing is unchecked, so I believe there is no need to wrap it. The issue is we need that custom data inside the exception, which is not a string message, to decide how we want to handle it.

I’ve updated the original message a bit to make it clearer.

To pass a custom data through an ApplicationFailure from the activity code explicitly. The ApplicationFailure factory method accepts the details arguments that can be accessed through ApplicationFailure.getDetails.