| | | 1 | | using Elsa.Extensions; |
| | | 2 | | using Elsa.Workflows.Pipelines.ActivityExecution; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Workflows.Middleware.Activities; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Adds extension methods to <see cref="ExceptionHandlingMiddleware"/>. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class ExceptionHandlingMiddlewareExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Installs the <see cref="ExceptionHandlingMiddleware"/> component in the activity execution pipeline. |
| | | 14 | | /// </summary> |
| | | 15 | | 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> |
| | 421 | 21 | | public class ExceptionHandlingMiddleware(ActivityMiddlewareDelegate next, IIncidentStrategyResolver incidentStrategyReso |
| | | 22 | | : IActivityExecutionMiddleware |
| | | 23 | | { |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public async ValueTask InvokeAsync(ActivityExecutionContext context) |
| | | 26 | | { |
| | | 27 | | try |
| | | 28 | | { |
| | 3019 | 29 | | await next(context); |
| | 3006 | 30 | | } |
| | 13 | 31 | | catch (Exception e) |
| | | 32 | | { |
| | 13 | 33 | | logger.LogWarning(e, "An exception was caught from a downstream middleware component"); |
| | 13 | 34 | | context.Fault(e); |
| | 13 | 35 | | await HandleIncidentAsync(context); |
| | | 36 | | } |
| | 3019 | 37 | | } |
| | | 38 | | |
| | | 39 | | private async Task HandleIncidentAsync(ActivityExecutionContext context) |
| | | 40 | | { |
| | 13 | 41 | | var strategy = await incidentStrategyResolver.ResolveStrategyAsync(context); |
| | 13 | 42 | | strategy.HandleIncident(context); |
| | 13 | 43 | | } |
| | | 44 | | } |