How to catch ActivityError or ApplicationError from the result() of get_workflow_handle()?

Occasionally, I have a workflow that does not complete successfully ( Failed status). I’m trying to capture the actual details around the failure (which is associated with an ApplicationError exception) from the result() method associated with get_workflow_handle().

However, that method throws a WorkflowFailureError client exception similar to the following:

ApplicationError                          Traceback (most recent call last)
ApplicationError: MyCustomException: Failed to fix aspect: 30800000-4343-4653-3344-4d3134304b30:e_link_width : MyCustomException(StatusCode.NOT_FOUND, Did not find alert to update!)

The above exception was the direct cause of the following exception:

ActivityError                             Traceback (most recent call last)
ActivityError: Activity task failed

The above exception was the direct cause of the following exception:

WorkflowFailureError                      Traceback (most recent call last)

How can I get the ApplicationError exception? I can’t catch that exception from result().

The pasted snippet shows that WorkflowFailureError has a cause of ActivityError which has a cause of ApplicationError. You may get the cause of WorkflowFailureError with the cause property (which is just a friendly name for the built-in __cause__ attribute). And then you can get that one’s cause too. If you don’t want it wrapped in an ActivityError, you can catch and re-raise in the workflow with an ApplicationError directly. Note this error also lets you provide arbitrary objects as details which can be retrieved client side.

Ah, thanks, I see now. If I catch WorkflowFailureError as err then I can get the actual ApplicationError message via err.cause.cause.message