< Summary

Information
Class: Elsa.Workflows.Middleware.Workflows.EngineExceptionHandlingMiddleware
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Middleware/Workflows/EngineExceptionHandlingMiddleware.cs
Line coverage
30%
Covered lines: 4
Uncovered lines: 9
Coverable lines: 13
Total lines: 46
Line coverage: 30.7%
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
.ctor(...)100%11100%
InvokeAsync()100%1125%

File(s)

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

#LineLine coverage
 1using Elsa.Common;
 2using Elsa.Workflows.Models;
 3using Elsa.Workflows.Pipelines.WorkflowExecution;
 4using Elsa.Workflows.State;
 5using Microsoft.Extensions.Logging;
 6
 7namespace Elsa.Workflows.Middleware.Workflows;
 8
 9/// <summary>
 10/// Adds extension methods to <see cref="ExceptionHandlingMiddleware"/>.
 11/// </summary>
 12public static class EngineExceptionHandlingMiddlewareExtensions
 13{
 14    /// <summary>
 15    /// Installs the <see cref="ExceptionHandlingMiddleware"/> component in the activity execution pipeline.
 16    /// </summary>
 17    public static IWorkflowExecutionPipelineBuilder UseEngineExceptionHandling(this IWorkflowExecutionPipelineBuilder pi
 18}
 19
 20/// <summary>
 21/// Catches any exceptions thrown by downstream components and transitions the workflow into the faulted state.
 22/// </summary>
 42523public class EngineExceptionHandlingMiddleware(WorkflowMiddlewareDelegate next, ISystemClock systemClock, ILogger<Engine
 24{
 25    /// <inheritdoc />
 26    public async ValueTask InvokeAsync(WorkflowExecutionContext context)
 27    {
 28        try
 29        {
 44030            await next(context);
 44031        }
 032        catch (Exception e)
 33        {
 034            logger.LogWarning(e, "An exception was caught from a downstream middleware component");
 035            var exceptionState = ExceptionState.FromException(e);
 036            var now = systemClock.UtcNow;
 037            var activity = context.Workflow;
 038            var incident = new ActivityIncident(activity.Id, activity.NodeId, activity.Type, e.Message, exceptionState, 
 39
 40            // No state change as the workflow / activities status should be leading.
 41            // We will however be adding an incident to make the issue visible.
 042            context.Incidents.Add(incident);
 043            context.AddExecutionLogEntry("Faulted", e.Message, exceptionState);
 044        }
 44045    }
 46}