< Summary

Information
Class: Elsa.Workflows.Middleware.Workflows.ExceptionHandlingMiddlewareExtensions
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Middleware/Workflows/ExceptionHandlingMiddleware.cs
Line coverage
100%
Covered lines: 1
Uncovered lines: 0
Coverable lines: 1
Total lines: 58
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/Workflows/ExceptionHandlingMiddleware.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 ExceptionHandlingMiddlewareExtensions
 13{
 14    /// <summary>
 15    /// Installs the <see cref="ExceptionHandlingMiddleware"/> component in the activity execution pipeline.
 16    /// </summary>
 42517    public static IWorkflowExecutionPipelineBuilder UseExceptionHandling(this IWorkflowExecutionPipelineBuilder pipeline
 18}
 19
 20/// <summary>
 21/// Catches any exceptions thrown by downstream components and transitions the workflow into the faulted state.
 22/// </summary>
 23public class ExceptionHandlingMiddleware : IWorkflowExecutionMiddleware
 24{
 25    private readonly WorkflowMiddlewareDelegate _next;
 26    private readonly ISystemClock _systemClock;
 27    private readonly ILogger<ExceptionHandlingMiddleware> _logger;
 28
 29    /// <summary>
 30    /// Constructor.
 31    /// </summary>
 32    public ExceptionHandlingMiddleware(WorkflowMiddlewareDelegate next, ISystemClock systemClock, ILogger<ExceptionHandl
 33    {
 34        _next = next;
 35        _systemClock = systemClock;
 36        _logger = logger;
 37    }
 38
 39    /// <inheritdoc />
 40    public async ValueTask InvokeAsync(WorkflowExecutionContext context)
 41    {
 42        try
 43        {
 44            await _next(context);
 45        }
 46        catch (Exception e)
 47        {
 48            _logger.LogWarning(e, "An exception was caught from a downstream middleware component");
 49            var exceptionState = ExceptionState.FromException(e);
 50            var now = _systemClock.UtcNow;
 51            var activity = context.Workflow;
 52            var incident = new ActivityIncident(activity.Id, activity.NodeId ,activity.Type, e.Message, exceptionState, 
 53            context.Incidents.Add(incident);
 54            context.TransitionTo(WorkflowSubStatus.Faulted);
 55            context.AddExecutionLogEntry("Faulted", e.Message, exceptionState);
 56        }
 57    }
 58}