Error handling in Azure Durable Functions
    • Dark
      Light
    • PDF

    Error handling in Azure Durable Functions

    • Dark
      Light
    • PDF

    Article Summary

    #ServerlessTips - Azure Functions
    Author: Steef-Jan Wiggers Azure MVP

    With programming a workflow in Durable Function you need to deal with errors. Somewhere in your flow, an error can occur. For instance, when your orchestrator function calls an activity function, and within that function, a time-out exception happens.

    When an exception occurs in an activity function, the exception will be marshalled back to the orchestrator – these exceptions are thrown as FunctionFailedExceptions.

    Moreover, you can deal with these exceptions in the orchestrator.

    public static async Task Run(DurableOrchestrationContext context)
    {
        try
        {
            var details = ctx.GetInput<Details>();
            await context.CallActivityAsync("Monitor", details);
        }
        catch (FunctionFailedException)
        {
            var otherDetails = GetOtherInput(/*.....*/);
            await context.CallActivityAsync("Monitor", otherDetails);
        }
    }
    

    The code above shows to call an activity function, and if that fails with the given details, you can see another call to the activity again with other information. In case, you need to retry with the same information than you need to implement retries.

    Dealing with errors can take place in the orchestrator. However, you can within the activity function deal with the error first. For instance, you could try some cleanup work or send something more useful back to the orchestrator.

    public static async Task<object> Monitor(
        [ActivityTrigger] string details,
        TraceWriter log)
    {
        try
        {
            var myOutputData = await DoSomething(details);
            return new 
            {
                Success = true,
                Result = myOutputData
            };
        }
        catch (Exception e)
        {
            // optionally do some cleanup work ...
            DoCleanup();
            return new 
            {
                Success = false,
                ErrorMessage = e.Message
            };
        }
    }
    
    If an orchestrator function fails in case, there is no error handling implemented the details of the exception are logged, and the instance completes with a Failed status.

    Was this article helpful?