Python SDK - how to catch "temporalio.exceptions.ApplicationError"

Hi,

I have ready workflow and activities implemented in go. However due to configuration of envirotment reasons I have to trigger workflow from Python.

And from Python I would like to catch “temporalio.exceptions.ApplicationError” and its value. Is there any way to do it prettily using SDK?

tst

Sure, you should be able to just catch the result. For example:

try:
    my_result = await my_client.get_workflow_handle("my-workflow-id").result()
    print(f"Successful result: {my_result}")
except WorkflowFailureError as err:
    if instanceof(err.cause, ApplicationError):
        print(f"Workflow failed with application error: {err.cause}")
    else:
        print(f"Workflow failed with a non-application error: {err.cause}")

(edit: fixed example)

Hi,

Thank you for the swift reply.
Fixed example works better (I understand you meant “isinstance” and not “instanceof”) however I still catch activity error and not application error.

Workflow failed with a non-application error: activity error.

Traceback shows ApplicationError:

temporalio.exceptions.ApplicationError: Validation Error:

Ah, I was under the impression that your workflow failed with an application error, not your activity that you’re just bubbling up through the workflow.

If you returned an ApplicationError from the workflow, the client will see WorkflowFailureError with cause ApplicationError. If you returned an ApplicationError from the activity, the workflow will see ActivityError with cause ApplicationError. If you rethrow that same activity error, the client will see WorkflowFailureError with cause ActivityError with cause ApplicationError.

We intentionally wrap workflow and activity errors as they are received by the caller so, in cases like these where you rethrow them without wrapping, there is no ambiguity about where they came from.

So if you must react to an activity error outside of the workflow, best practice is to react in the workflow maybe with a clearer error to the workflow caller. But if you really want to see the activity error rethrown by the workflow, you can just obtain the err.cause.cause instead.

That make sense indeed. Somehow I have epxected that exactly Application Error, will bubble up.
Thanks again for swift reply!