From ddf1d6385461f0e06e6c1c8f5505c24c2676b8ce Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 17 Feb 2026 15:38:19 +0000 Subject: [PATCH] Add error handling to additional workflow samples with agents Co-authored-by: lokitoth <6936551+lokitoth@users.noreply.github.com> --- .../Concurrent/Concurrent/Program.cs | 18 +++++++++++-- .../01_EdgeCondition/Program.cs | 18 +++++++++++-- .../ConditionalEdges/02_SwitchCase/Program.cs | 18 +++++++++++-- .../03_MultiSelection/Program.cs | 25 ++++++++++++++----- 4 files changed, 67 insertions(+), 12 deletions(-) diff --git a/dotnet/samples/GettingStarted/Workflows/Concurrent/Concurrent/Program.cs b/dotnet/samples/GettingStarted/Workflows/Concurrent/Concurrent/Program.cs index bed7d3fe6d..438fc6af77 100644 --- a/dotnet/samples/GettingStarted/Workflows/Concurrent/Concurrent/Program.cs +++ b/dotnet/samples/GettingStarted/Workflows/Concurrent/Concurrent/Program.cs @@ -64,9 +64,23 @@ public static class Program await using StreamingRun run = await InProcessExecution.StreamAsync(workflow, input: "What is temperature?"); await foreach (WorkflowEvent evt in run.WatchStreamAsync()) { - if (evt is WorkflowOutputEvent output) + switch (evt) { - Console.WriteLine($"Workflow completed with results:\n{output.Data}"); + case WorkflowOutputEvent output: + Console.WriteLine($"Workflow completed with results:\n{output.Data}"); + break; + + case ExecutorFailedEvent failureEvent: + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"Executor failed [{failureEvent.ExecutorId}]: {failureEvent.Data?.Message ?? "Unknown error"}"); + Console.ResetColor(); + break; + + case WorkflowErrorEvent errorEvent: + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"Workflow error: {errorEvent.Exception?.Message ?? "Unknown error"}"); + Console.ResetColor(); + throw errorEvent.Exception ?? new InvalidOperationException("Workflow encountered an error."); } } } diff --git a/dotnet/samples/GettingStarted/Workflows/ConditionalEdges/01_EdgeCondition/Program.cs b/dotnet/samples/GettingStarted/Workflows/ConditionalEdges/01_EdgeCondition/Program.cs index a04f081ef2..0a56383bd9 100644 --- a/dotnet/samples/GettingStarted/Workflows/ConditionalEdges/01_EdgeCondition/Program.cs +++ b/dotnet/samples/GettingStarted/Workflows/ConditionalEdges/01_EdgeCondition/Program.cs @@ -68,9 +68,23 @@ public static class Program await run.TrySendMessageAsync(new TurnToken(emitEvents: true)); await foreach (WorkflowEvent evt in run.WatchStreamAsync()) { - if (evt is WorkflowOutputEvent outputEvent) + switch (evt) { - Console.WriteLine($"{outputEvent}"); + case WorkflowOutputEvent outputEvent: + Console.WriteLine($"{outputEvent}"); + break; + + case ExecutorFailedEvent failureEvent: + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"Executor failed [{failureEvent.ExecutorId}]: {failureEvent.Data?.Message ?? "Unknown error"}"); + Console.ResetColor(); + break; + + case WorkflowErrorEvent errorEvent: + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"Workflow error: {errorEvent.Exception?.Message ?? "Unknown error"}"); + Console.ResetColor(); + throw errorEvent.Exception ?? new InvalidOperationException("Workflow encountered an error."); } } } diff --git a/dotnet/samples/GettingStarted/Workflows/ConditionalEdges/02_SwitchCase/Program.cs b/dotnet/samples/GettingStarted/Workflows/ConditionalEdges/02_SwitchCase/Program.cs index 4919bcea09..6d73bb8562 100644 --- a/dotnet/samples/GettingStarted/Workflows/ConditionalEdges/02_SwitchCase/Program.cs +++ b/dotnet/samples/GettingStarted/Workflows/ConditionalEdges/02_SwitchCase/Program.cs @@ -84,9 +84,23 @@ public static class Program await run.TrySendMessageAsync(new TurnToken(emitEvents: true)); await foreach (WorkflowEvent evt in run.WatchStreamAsync()) { - if (evt is WorkflowOutputEvent outputEvent) + switch (evt) { - Console.WriteLine($"{outputEvent}"); + case WorkflowOutputEvent outputEvent: + Console.WriteLine($"{outputEvent}"); + break; + + case ExecutorFailedEvent failureEvent: + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"Executor failed [{failureEvent.ExecutorId}]: {failureEvent.Data?.Message ?? "Unknown error"}"); + Console.ResetColor(); + break; + + case WorkflowErrorEvent errorEvent: + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"Workflow error: {errorEvent.Exception?.Message ?? "Unknown error"}"); + Console.ResetColor(); + throw errorEvent.Exception ?? new InvalidOperationException("Workflow encountered an error."); } } } diff --git a/dotnet/samples/GettingStarted/Workflows/ConditionalEdges/03_MultiSelection/Program.cs b/dotnet/samples/GettingStarted/Workflows/ConditionalEdges/03_MultiSelection/Program.cs index 32a727f8b7..ba3eac36cf 100644 --- a/dotnet/samples/GettingStarted/Workflows/ConditionalEdges/03_MultiSelection/Program.cs +++ b/dotnet/samples/GettingStarted/Workflows/ConditionalEdges/03_MultiSelection/Program.cs @@ -92,14 +92,27 @@ public static class Program await run.TrySendMessageAsync(new TurnToken(emitEvents: true)); await foreach (WorkflowEvent evt in run.WatchStreamAsync()) { - if (evt is WorkflowOutputEvent outputEvent) + switch (evt) { - Console.WriteLine($"{outputEvent}"); - } + case WorkflowOutputEvent outputEvent: + Console.WriteLine($"{outputEvent}"); + break; - if (evt is DatabaseEvent databaseEvent) - { - Console.WriteLine($"{databaseEvent}"); + case DatabaseEvent databaseEvent: + Console.WriteLine($"{databaseEvent}"); + break; + + case ExecutorFailedEvent failureEvent: + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"Executor failed [{failureEvent.ExecutorId}]: {failureEvent.Data?.Message ?? "Unknown error"}"); + Console.ResetColor(); + break; + + case WorkflowErrorEvent errorEvent: + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"Workflow error: {errorEvent.Exception?.Message ?? "Unknown error"}"); + Console.ResetColor(); + throw errorEvent.Exception ?? new InvalidOperationException("Workflow encountered an error."); } } }