< Summary

Information
Class: Elsa.Workflows.Middleware.Activities.ExceptionHandlingMiddlewareExtensions
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Middleware/Activities/ExceptionHandlingMiddleware.cs
Line coverage
100%
Covered lines: 1
Uncovered lines: 0
Coverable lines: 1
Total lines: 44
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
UseExceptionHandling(...)100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Middleware/Activities/ExceptionHandlingMiddleware.cs

#LineLine coverage
 1using Elsa.Extensions;
 2using Elsa.Workflows.Pipelines.ActivityExecution;
 3using Microsoft.Extensions.Logging;
 4
 5namespace Elsa.Workflows.Middleware.Activities;
 6
 7/// <summary>
 8/// Adds extension methods to <see cref="ExceptionHandlingMiddleware"/>.
 9/// </summary>
 10public static class ExceptionHandlingMiddlewareExtensions
 11{
 12    /// <summary>
 13    /// Installs the <see cref="ExceptionHandlingMiddleware"/> component in the activity execution pipeline.
 14    /// </summary>
 42115    public static IActivityExecutionPipelineBuilder UseExceptionHandling(this IActivityExecutionPipelineBuilder pipeline
 16}
 17
 18/// <summary>
 19/// Catches any exceptions thrown by downstream components and transitions the workflow into the faulted state.
 20/// </summary>
 21public class ExceptionHandlingMiddleware(ActivityMiddlewareDelegate next, IIncidentStrategyResolver incidentStrategyReso
 22    : IActivityExecutionMiddleware
 23{
 24    /// <inheritdoc />
 25    public async ValueTask InvokeAsync(ActivityExecutionContext context)
 26    {
 27        try
 28        {
 29            await next(context);
 30        }
 31        catch (Exception e)
 32        {
 33            logger.LogWarning(e, "An exception was caught from a downstream middleware component");
 34            context.Fault(e);
 35            await HandleIncidentAsync(context);
 36        }
 37    }
 38
 39    private async Task HandleIncidentAsync(ActivityExecutionContext context)
 40    {
 41        var strategy = await incidentStrategyResolver.ResolveStrategyAsync(context);
 42        strategy.HandleIncident(context);
 43    }
 44}