Skip to content

Commit ca580a8

Browse files
Copilotlokitoth
andauthored
.NET: Add error checking to workflow samples (#5175)
* Initial plan * Add WorkflowErrorEvent and ExecutorFailedEvent error checking to all workflow samples Agent-Logs-Url: https://github.com/microsoft/agent-framework/sessions/c5d77400-d7ed-4fbe-9103-f5d74aabcf2b Co-authored-by: lokitoth <6936551+lokitoth@users.noreply.github.com> * Fix if/else if consistency for error event handlers per code review feedback Agent-Logs-Url: https://github.com/microsoft/agent-framework/sessions/c5d77400-d7ed-4fbe-9103-f5d74aabcf2b Co-authored-by: lokitoth <6936551+lokitoth@users.noreply.github.com> * Address PR comments * fixup: PR comments --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: lokitoth <6936551+lokitoth@users.noreply.github.com> Co-authored-by: Jacob Alber <jaalber@microsoft.com>
1 parent 101e07b commit ca580a8

20 files changed

Lines changed: 323 additions & 44 deletions

File tree

dotnet/samples/03-workflows/Agents/FoundryAgent/Program.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ private static async Task Main()
5353
{
5454
Console.WriteLine($"{executorComplete.ExecutorId}: {executorComplete.Data}");
5555
}
56+
else if (evt is WorkflowErrorEvent workflowError)
57+
{
58+
Console.ForegroundColor = ConsoleColor.Red;
59+
Console.Error.WriteLine(workflowError.Exception?.ToString() ?? "Unknown workflow error occurred.");
60+
Console.ResetColor();
61+
}
62+
else if (evt is ExecutorFailedEvent executorFailed)
63+
{
64+
Console.ForegroundColor = ConsoleColor.Red;
65+
Console.Error.WriteLine($"Executor '{executorFailed.ExecutorId}' failed with {(executorFailed.Data == null ? "unknown error" : $"exception {executorFailed.Data}")}.");
66+
Console.ResetColor();
67+
}
5668
}
5769
}
5870
finally

dotnet/samples/03-workflows/Agents/GroupChatToolApproval/Program.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ private static async Task Main()
134134

135135
break;
136136
}
137+
138+
case WorkflowErrorEvent workflowError:
139+
Console.ForegroundColor = ConsoleColor.Red;
140+
Console.Error.WriteLine(workflowError.Exception?.ToString() ?? "Unknown workflow error occurred.");
141+
Console.ResetColor();
142+
break;
143+
144+
case ExecutorFailedEvent executorFailed:
145+
Console.ForegroundColor = ConsoleColor.Red;
146+
Console.Error.WriteLine($"Executor '{executorFailed.ExecutorId}' failed with {(executorFailed.Data == null ? "unknown error" : $"exception {executorFailed.Data}")}.");
147+
Console.ResetColor();
148+
break;
137149
}
138150
}
139151

dotnet/samples/03-workflows/Checkpoint/CheckpointAndRehydrate/Program.cs

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,41 @@ private static async Task Main()
3737

3838
await foreach (WorkflowEvent evt in checkpointedRun.WatchStreamAsync())
3939
{
40-
if (evt is ExecutorCompletedEvent executorCompletedEvt)
40+
switch (evt)
4141
{
42-
Console.WriteLine($"* Executor {executorCompletedEvt.ExecutorId} completed.");
43-
}
42+
case ExecutorCompletedEvent executorCompletedEvt:
43+
Console.WriteLine($"* Executor {executorCompletedEvt.ExecutorId} completed.");
44+
break;
4445

45-
if (evt is SuperStepCompletedEvent superStepCompletedEvt)
46-
{
47-
// Checkpoints are automatically created at the end of each super step when a
48-
// checkpoint manager is provided. You can store the checkpoint info for later use.
49-
CheckpointInfo? checkpoint = superStepCompletedEvt.CompletionInfo!.Checkpoint;
50-
if (checkpoint is not null)
46+
case SuperStepCompletedEvent superStepCompletedEvt:
5147
{
52-
checkpoints.Add(checkpoint);
53-
Console.WriteLine($"** Checkpoint created at step {checkpoints.Count}.");
48+
// Checkpoints are automatically created at the end of each super step when a
49+
// checkpoint manager is provided. You can store the checkpoint info for later use.
50+
CheckpointInfo? checkpoint = superStepCompletedEvt.CompletionInfo!.Checkpoint;
51+
if (checkpoint is not null)
52+
{
53+
checkpoints.Add(checkpoint);
54+
Console.WriteLine($"** Checkpoint created at step {checkpoints.Count}.");
55+
}
56+
57+
break;
5458
}
55-
}
5659

57-
if (evt is WorkflowOutputEvent outputEvent)
58-
{
59-
Console.WriteLine($"Workflow completed with result: {outputEvent.Data}");
60+
case WorkflowOutputEvent outputEvent:
61+
Console.WriteLine($"Workflow completed with result: {outputEvent.Data}");
62+
break;
63+
64+
case WorkflowErrorEvent workflowError:
65+
Console.ForegroundColor = ConsoleColor.Red;
66+
Console.Error.WriteLine(workflowError.Exception?.ToString() ?? "Unknown workflow error occurred.");
67+
Console.ResetColor();
68+
break;
69+
70+
case ExecutorFailedEvent executorFailed:
71+
Console.ForegroundColor = ConsoleColor.Red;
72+
Console.Error.WriteLine($"Executor '{executorFailed.ExecutorId}' failed with {(executorFailed.Data == null ? "unknown error" : $"exception {executorFailed.Data}")}.");
73+
Console.ResetColor();
74+
break;
6075
}
6176
}
6277

@@ -77,14 +92,27 @@ private static async Task Main()
7792

7893
await foreach (WorkflowEvent evt in newCheckpointedRun.WatchStreamAsync())
7994
{
80-
if (evt is ExecutorCompletedEvent executorCompletedEvt)
95+
switch (evt)
8196
{
82-
Console.WriteLine($"* Executor {executorCompletedEvt.ExecutorId} completed.");
83-
}
97+
case ExecutorCompletedEvent executorCompletedEvt:
98+
Console.WriteLine($"* Executor {executorCompletedEvt.ExecutorId} completed.");
99+
break;
84100

85-
if (evt is WorkflowOutputEvent workflowOutputEvt)
86-
{
87-
Console.WriteLine($"Workflow completed with result: {workflowOutputEvt.Data}");
101+
case WorkflowOutputEvent workflowOutputEvt:
102+
Console.WriteLine($"Workflow completed with result: {workflowOutputEvt.Data}");
103+
break;
104+
105+
case WorkflowErrorEvent workflowError:
106+
Console.ForegroundColor = ConsoleColor.Red;
107+
Console.Error.WriteLine(workflowError.Exception?.ToString() ?? "Unknown workflow error occurred.");
108+
Console.ResetColor();
109+
break;
110+
111+
case ExecutorFailedEvent executorFailed:
112+
Console.ForegroundColor = ConsoleColor.Red;
113+
Console.Error.WriteLine($"Executor '{executorFailed.ExecutorId}' failed with {(executorFailed.Data == null ? "unknown error" : $"exception {executorFailed.Data}")}.");
114+
Console.ResetColor();
115+
break;
88116
}
89117
}
90118
}

dotnet/samples/03-workflows/Checkpoint/CheckpointAndResume/Program.cs

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,41 @@ private static async Task Main()
3434
await using StreamingRun checkpointedRun = await InProcessExecution.RunStreamingAsync(workflow, NumberSignal.Init, checkpointManager);
3535
await foreach (WorkflowEvent evt in checkpointedRun.WatchStreamAsync())
3636
{
37-
if (evt is ExecutorCompletedEvent executorCompletedEvt)
37+
switch (evt)
3838
{
39-
Console.WriteLine($"* Executor {executorCompletedEvt.ExecutorId} completed.");
40-
}
39+
case ExecutorCompletedEvent executorCompletedEvt:
40+
Console.WriteLine($"* Executor {executorCompletedEvt.ExecutorId} completed.");
41+
break;
4142

42-
if (evt is SuperStepCompletedEvent superStepCompletedEvt)
43-
{
44-
// Checkpoints are automatically created at the end of each super step when a
45-
// checkpoint manager is provided. You can store the checkpoint info for later use.
46-
CheckpointInfo? checkpoint = superStepCompletedEvt.CompletionInfo!.Checkpoint;
47-
if (checkpoint is not null)
43+
case SuperStepCompletedEvent superStepCompletedEvt:
4844
{
49-
checkpoints.Add(checkpoint);
50-
Console.WriteLine($"** Checkpoint created at step {checkpoints.Count}.");
45+
// Checkpoints are automatically created at the end of each super step when a
46+
// checkpoint manager is provided. You can store the checkpoint info for later use.
47+
CheckpointInfo? checkpoint = superStepCompletedEvt.CompletionInfo!.Checkpoint;
48+
if (checkpoint is not null)
49+
{
50+
checkpoints.Add(checkpoint);
51+
Console.WriteLine($"** Checkpoint created at step {checkpoints.Count}.");
52+
}
53+
54+
break;
5155
}
52-
}
5356

54-
if (evt is WorkflowOutputEvent workflowOutputEvt)
55-
{
56-
Console.WriteLine($"Workflow completed with result: {workflowOutputEvt.Data}");
57+
case WorkflowOutputEvent outputEvent:
58+
Console.WriteLine($"Workflow completed with result: {outputEvent.Data}");
59+
break;
60+
61+
case WorkflowErrorEvent workflowError:
62+
Console.ForegroundColor = ConsoleColor.Red;
63+
Console.Error.WriteLine(workflowError.Exception?.ToString() ?? "Unknown workflow error occurred.");
64+
Console.ResetColor();
65+
break;
66+
67+
case ExecutorFailedEvent executorFailed:
68+
Console.ForegroundColor = ConsoleColor.Red;
69+
Console.Error.WriteLine($"Executor '{executorFailed.ExecutorId}' failed with {(executorFailed.Data == null ? "unknown error" : $"exception {executorFailed.Data}")}.");
70+
Console.ResetColor();
71+
break;
5772
}
5873
}
5974

@@ -71,14 +86,27 @@ private static async Task Main()
7186
await checkpointedRun.RestoreCheckpointAsync(savedCheckpoint, CancellationToken.None);
7287
await foreach (WorkflowEvent evt in checkpointedRun.WatchStreamAsync())
7388
{
74-
if (evt is ExecutorCompletedEvent executorCompletedEvt)
89+
switch (evt)
7590
{
76-
Console.WriteLine($"* Executor {executorCompletedEvt.ExecutorId} completed.");
77-
}
91+
case ExecutorCompletedEvent executorCompletedEvt:
92+
Console.WriteLine($"* Executor {executorCompletedEvt.ExecutorId} completed.");
93+
break;
7894

79-
if (evt is WorkflowOutputEvent workflowOutputEvt)
80-
{
81-
Console.WriteLine($"Workflow completed with result: {workflowOutputEvt.Data}");
95+
case WorkflowOutputEvent workflowOutputEvt:
96+
Console.WriteLine($"Workflow completed with result: {workflowOutputEvt.Data}");
97+
break;
98+
99+
case WorkflowErrorEvent workflowError:
100+
Console.ForegroundColor = ConsoleColor.Red;
101+
Console.Error.WriteLine(workflowError.Exception?.ToString() ?? "Unknown workflow error occurred.");
102+
Console.ResetColor();
103+
break;
104+
105+
case ExecutorFailedEvent executorFailed:
106+
Console.ForegroundColor = ConsoleColor.Red;
107+
Console.Error.WriteLine($"Executor '{executorFailed.ExecutorId}' failed with {(executorFailed.Data == null ? "unknown error" : $"exception {executorFailed.Data}")}.");
108+
Console.ResetColor();
109+
break;
82110
}
83111
}
84112
}

dotnet/samples/03-workflows/Checkpoint/CheckpointWithHumanInTheLoop/Program.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ private static async Task Main()
6262
case WorkflowOutputEvent workflowOutputEvt:
6363
Console.WriteLine($"Workflow completed with result: {workflowOutputEvt.Data}");
6464
break;
65+
case WorkflowErrorEvent workflowError:
66+
Console.ForegroundColor = ConsoleColor.Red;
67+
Console.Error.WriteLine(workflowError.Exception?.ToString() ?? "Unknown workflow error occurred.");
68+
Console.ResetColor();
69+
break;
70+
case ExecutorFailedEvent executorFailed:
71+
Console.ForegroundColor = ConsoleColor.Red;
72+
Console.Error.WriteLine($"Executor '{executorFailed.ExecutorId}' failed with {(executorFailed.Data == null ? "unknown error" : $"exception {executorFailed.Data}")}.");
73+
Console.ResetColor();
74+
break;
6575
}
6676
}
6777

@@ -92,6 +102,16 @@ private static async Task Main()
92102
case WorkflowOutputEvent workflowOutputEvt:
93103
Console.WriteLine($"Workflow completed with result: {workflowOutputEvt.Data}");
94104
break;
105+
case WorkflowErrorEvent workflowError:
106+
Console.ForegroundColor = ConsoleColor.Red;
107+
Console.Error.WriteLine(workflowError.Exception?.ToString() ?? "Unknown workflow error occurred.");
108+
Console.ResetColor();
109+
break;
110+
case ExecutorFailedEvent executorFailed:
111+
Console.ForegroundColor = ConsoleColor.Red;
112+
Console.Error.WriteLine($"Executor '{executorFailed.ExecutorId}' failed with {(executorFailed.Data == null ? "unknown error" : $"exception {executorFailed.Data}")}.");
113+
Console.ResetColor();
114+
break;
95115
}
96116
}
97117
}

dotnet/samples/03-workflows/Concurrent/MapReduce/Program.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ private static async Task RunWorkflowAsync(Workflow workflow)
119119
}
120120
}
121121
}
122+
else if (evt is WorkflowErrorEvent workflowError)
123+
{
124+
Console.ForegroundColor = ConsoleColor.Red;
125+
Console.Error.WriteLine(workflowError.Exception?.ToString() ?? "Unknown workflow error occurred.");
126+
Console.ResetColor();
127+
}
128+
else if (evt is ExecutorFailedEvent executorFailed)
129+
{
130+
Console.ForegroundColor = ConsoleColor.Red;
131+
Console.Error.WriteLine($"Executor '{executorFailed.ExecutorId}' failed with {(executorFailed.Data == null ? "unknown error" : $"exception {executorFailed.Data}")}.");
132+
Console.ResetColor();
133+
}
122134
}
123135
}
124136
}

dotnet/samples/03-workflows/ConditionalEdges/01_EdgeCondition/Program.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ private static async Task Main()
6969
{
7070
Console.WriteLine($"{outputEvent}");
7171
}
72+
else if (evt is WorkflowErrorEvent workflowError)
73+
{
74+
Console.ForegroundColor = ConsoleColor.Red;
75+
Console.Error.WriteLine(workflowError.Exception?.ToString() ?? "Unknown workflow error occurred.");
76+
Console.ResetColor();
77+
}
78+
else if (evt is ExecutorFailedEvent executorFailed)
79+
{
80+
Console.ForegroundColor = ConsoleColor.Red;
81+
Console.Error.WriteLine($"Executor '{executorFailed.ExecutorId}' failed with {(executorFailed.Data == null ? "unknown error" : $"exception {executorFailed.Data}")}.");
82+
Console.ResetColor();
83+
}
7284
}
7385
}
7486

dotnet/samples/03-workflows/ConditionalEdges/02_SwitchCase/Program.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ private static async Task Main()
8585
{
8686
Console.WriteLine($"{outputEvent}");
8787
}
88+
else if (evt is WorkflowErrorEvent workflowError)
89+
{
90+
Console.ForegroundColor = ConsoleColor.Red;
91+
Console.Error.WriteLine(workflowError.Exception?.ToString() ?? "Unknown workflow error occurred.");
92+
Console.ResetColor();
93+
}
94+
else if (evt is ExecutorFailedEvent executorFailed)
95+
{
96+
Console.ForegroundColor = ConsoleColor.Red;
97+
Console.Error.WriteLine($"Executor '{executorFailed.ExecutorId}' failed with {(executorFailed.Data == null ? "unknown error" : $"exception {executorFailed.Data}")}.");
98+
Console.ResetColor();
99+
}
88100
}
89101
}
90102

dotnet/samples/03-workflows/ConditionalEdges/03_MultiSelection/Program.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,22 @@ private static async Task Main()
9393
{
9494
Console.WriteLine($"{outputEvent}");
9595
}
96-
97-
if (evt is DatabaseEvent databaseEvent)
96+
else if (evt is DatabaseEvent databaseEvent)
9897
{
9998
Console.WriteLine($"{databaseEvent}");
10099
}
100+
else if (evt is WorkflowErrorEvent workflowError)
101+
{
102+
Console.ForegroundColor = ConsoleColor.Red;
103+
Console.Error.WriteLine(workflowError.Exception?.ToString() ?? "Unknown workflow error occurred.");
104+
Console.ResetColor();
105+
}
106+
else if (evt is ExecutorFailedEvent executorFailed)
107+
{
108+
Console.ForegroundColor = ConsoleColor.Red;
109+
Console.Error.WriteLine($"Executor '{executorFailed.ExecutorId}' failed with {(executorFailed.Data == null ? "unknown error" : $"exception {executorFailed.Data}")}.");
110+
Console.ResetColor();
111+
}
101112
}
102113
}
103114

dotnet/samples/03-workflows/HumanInTheLoop/HumanInTheLoopBasic/Program.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ private static async Task Main()
4242
// The workflow has yielded output
4343
Console.WriteLine($"Workflow completed with result: {outputEvt.Data}");
4444
return;
45+
46+
case WorkflowErrorEvent workflowError:
47+
Console.ForegroundColor = ConsoleColor.Red;
48+
Console.Error.WriteLine(workflowError.Exception?.ToString() ?? "Unknown workflow error occurred.");
49+
Console.ResetColor();
50+
return;
51+
52+
case ExecutorFailedEvent executorFailed:
53+
Console.ForegroundColor = ConsoleColor.Red;
54+
Console.Error.WriteLine($"Executor '{executorFailed.ExecutorId}' failed with {(executorFailed.Data == null ? "unknown error" : $"exception {executorFailed.Data}")}.");
55+
Console.ResetColor();
56+
return;
4557
}
4658
}
4759
}

0 commit comments

Comments
 (0)