Skip to content

Instantly share code, notes, and snippets.

@ricardolsmendes
Last active April 18, 2021 04:58
Show Gist options
  • Save ricardolsmendes/8ff4f8582f8d9d906aad4668ebb2c767 to your computer and use it in GitHub Desktop.
Save ricardolsmendes/8ff4f8582f8d9d906aad4668ebb2c767 to your computer and use it in GitHub Desktop.
Exception handling for Azure Durable Functions in Python
try:
# Orchestration code here...
except Exception as e:
# Any exception that is raised by a function is re-raised by its enclosing
# activity task. The original error message is part of the new exception
# message, formatted as "Activity function '<function_name>' failed:
# <exception_type>: <exception_message>", so a regex is used to extract the
# original message and generate a friendlier output.
function_name = None
function_exception_type = None
function_exception_msg = None
activity_exception_msg = ''.join(e.args)
activity_exception_matcher = re.search(
r"Activity[\s]*function[\s]*'(?P<function_name>.+?)'[\s]*failed:"
r"[\s]*(?P<exception_type>.+?):"
r"[\s]*(?P<exception_message>.+?)[\s]*\n", activity_exception_msg)
if activity_exception_matcher:
function_name = activity_exception_matcher.group('function_name')
function_exception_type = \
activity_exception_matcher.group('exception_type')
function_exception_msg = \
activity_exception_matcher.group('exception_message')
context.set_custom_status(function_exception_msg)
orchestrator_msg = f"'{function_name}' raised {function_exception_type} >> " \
f" {function_exception_msg}'" \
if function_exception_msg else activity_exception_msg
output.append(orchestrator_msg)
raise Exception(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment