| | | 1 | | namespace NexusLabs.Needlr.AgentFramework.Workflows.Sequential; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Wraps an inner executor, catching exceptions and returning a failed result |
| | | 5 | | /// instead of throwing. This enables "advisory" stage behavior where a failure |
| | | 6 | | /// does not halt the pipeline. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <param name="inner">The executor to wrap.</param> |
| | | 9 | | /// <param name="onFailure">Optional callback invoked when the inner executor throws.</param> |
| | | 10 | | /// <example> |
| | | 11 | | /// <code> |
| | | 12 | | /// var safeExecutor = new ContinueOnFailureExecutor( |
| | | 13 | | /// innerExecutor, |
| | | 14 | | /// ex => logger.LogWarning(ex, "Stage failed but continuing")); |
| | | 15 | | /// |
| | | 16 | | /// var result = await safeExecutor.ExecuteAsync(context, cancellationToken); |
| | | 17 | | /// // result.Succeeded == false if the inner executor threw, but no exception propagates. |
| | | 18 | | /// </code> |
| | | 19 | | /// </example> |
| | | 20 | | [DoNotAutoRegister] |
| | 12 | 21 | | public sealed class ContinueOnFailureExecutor( |
| | 12 | 22 | | IStageExecutor inner, |
| | 12 | 23 | | Action<Exception>? onFailure = null) : IStageExecutor |
| | | 24 | | { |
| | | 25 | | /// <inheritdoc /> |
| | | 26 | | public async Task<StageExecutionResult> ExecuteAsync( |
| | | 27 | | StageExecutionContext context, |
| | | 28 | | CancellationToken cancellationToken) |
| | | 29 | | { |
| | | 30 | | try |
| | | 31 | | { |
| | 9 | 32 | | return await inner.ExecuteAsync(context, cancellationToken); |
| | | 33 | | } |
| | 3 | 34 | | catch (OperationCanceledException) when (context.CallerCancellationToken.IsCancellationRequested) |
| | | 35 | | { |
| | 1 | 36 | | throw; |
| | | 37 | | } |
| | 5 | 38 | | catch (Exception ex) |
| | | 39 | | { |
| | 5 | 40 | | onFailure?.Invoke(ex); |
| | 5 | 41 | | return StageExecutionResult.Failed( |
| | 5 | 42 | | context.StageName, |
| | 5 | 43 | | ex, |
| | 5 | 44 | | disposition: FailureDisposition.ContinueAdvisory); |
| | | 45 | | } |
| | 8 | 46 | | } |
| | | 47 | | } |