- Print
- DarkLight
- PDF
Error handling in Azure Durable Functions
- Print
- DarkLight
- PDF
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.
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
};
}
}