| | | 1 | | using Elsa.Common; |
| | | 2 | | using Elsa.Workflows.Models; |
| | | 3 | | using Elsa.Workflows.Pipelines.WorkflowExecution; |
| | | 4 | | using Elsa.Workflows.State; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows.Middleware.Workflows; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Adds extension methods to <see cref="ExceptionHandlingMiddleware"/>. |
| | | 11 | | /// </summary> |
| | | 12 | | public static class EngineExceptionHandlingMiddlewareExtensions |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Installs the <see cref="ExceptionHandlingMiddleware"/> component in the activity execution pipeline. |
| | | 16 | | /// </summary> |
| | 425 | 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> |
| | | 23 | | public class EngineExceptionHandlingMiddleware(WorkflowMiddlewareDelegate next, ISystemClock systemClock, ILogger<Engine |
| | | 24 | | { |
| | | 25 | | /// <inheritdoc /> |
| | | 26 | | public async ValueTask InvokeAsync(WorkflowExecutionContext context) |
| | | 27 | | { |
| | | 28 | | try |
| | | 29 | | { |
| | | 30 | | await next(context); |
| | | 31 | | } |
| | | 32 | | catch (Exception e) |
| | | 33 | | { |
| | | 34 | | logger.LogWarning(e, "An exception was caught from a downstream middleware component"); |
| | | 35 | | var exceptionState = ExceptionState.FromException(e); |
| | | 36 | | var now = systemClock.UtcNow; |
| | | 37 | | var activity = context.Workflow; |
| | | 38 | | 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. |
| | | 42 | | context.Incidents.Add(incident); |
| | | 43 | | context.AddExecutionLogEntry("Faulted", e.Message, exceptionState); |
| | | 44 | | } |
| | | 45 | | } |
| | | 46 | | } |