Azure / Azure/durabletask

Middleware for exceptions handling

Open
#1,039 4 comments 0 reactions 0 assignees View on GitHub
dt.core enhancement help wanted
Dominant language
C#
Stars
1.7k
Forks
335
Avg merge
2d 23h
Merged PRs (30d)
6

Description

I've created a middleware to centrally handle various types of exceptions.
Before my change, I had a try-catch block in my `TaskActivity.Execute()` method:

```csharp
protected override string Execute(TaskContext context, string input)
{
try
{
...
}
catch (BadExceptionType e)
{
throw new GoodExceptionType();
}
}
```
This worked fine regarding retries when an exception is thrown.
However, when I moved this logic to a middleware, RetryOptions were disregarded, and the same activity was retried over-and-over indefinitely:

``` csharp
public override async Task Dispatch(DispatchMiddlewareContext context, Func next)
{
try
{
await next();
}
catch (BadExceptionType e)
{
throw new GoodExceptionType();
}
}
```

Is there some guidance on handling exceptions in middleware? Is there a correct way to handle this scenario?
I've tried simulating the behavior of the dispatcher, and managed to get this to work, but that feels incorrect:

``` csharp
public override async Task Dispatch(DispatchMiddlewareContext context, Func next)
{
try
{
await next();
}
catch (Exception e)
{
var tfe = (TaskFailureException)e;

var failureEvent = new TaskFailedEvent(-1, taskScheduledEvent.EventId, tfe!.InnerException!.Message, null, new FailureDetails(tfe!.InnerException));
var result = new ActivityExecutionResult { ResponseEvent = failureEvent };
context.SetProperty(result);
}
}
```

@cgillum @davidmrdavid

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.