Skip to content

Commit

Permalink
Merge pull request #108 from ethanmoffat/eobot_error_handling
Browse files Browse the repository at this point in the history
Ensure exceptions result in an error return code. Ensure errors from all bots are properly displayed.
  • Loading branch information
ethanmoffat committed Feb 12, 2022
2 parents 720aa58 + 8a7ba1d commit 92ee845
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Expand Up @@ -27,7 +27,7 @@
"stopAtEntry": false
},
{
"name": ".NET Core Launch - Windows",
"name": "EndlessClient - Windows",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-client-windows",
Expand Down
15 changes: 14 additions & 1 deletion EOBot/BotFramework.cs
@@ -1,6 +1,7 @@
using EOLib.Net.Handlers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -105,7 +106,19 @@ public async Task RunAsync()
botTasks.Add(_botsList[i].RunAsync(_cancellationTokenSource.Token));
}

await Task.WhenAll(botTasks).ConfigureAwait(false);
// this is done to force handling of exceptions as an Aggregate exception so errors from multiple bots are shown properly
// otherwise, only the first exception from the first faulting task will be thrown
var continuation = Task.WhenAll(botTasks);
try
{
await continuation;
}
catch { }

if (continuation.Status != TaskStatus.RanToCompletion && continuation.Exception != null)
{
throw continuation.Exception;
}
}

public void TerminateBots()
Expand Down
16 changes: 15 additions & 1 deletion EOBot/Program.cs
Expand Up @@ -210,15 +210,29 @@ static async Task<int> Main(string[] args)
catch (BotException bex)
{
ConsoleHelper.WriteMessage(ConsoleHelper.Type.Error, bex.Message, ConsoleColor.DarkRed);
return 1;
}
catch (BotScriptErrorException bse)
{
ConsoleHelper.WriteMessage(ConsoleHelper.Type.Error, bse.Message, ConsoleColor.DarkRed);
return 1;
}
catch (AggregateException ae)
{
var botExceptions = ae.InnerExceptions.OfType<BotScriptErrorException>().ToList();
foreach (var ie in botExceptions)
ConsoleHelper.WriteMessage(ConsoleHelper.Type.Error, ie.Message, ConsoleColor.DarkRed);

var otherExceptions = ae.InnerExceptions.Except(botExceptions);
foreach (var ie in otherExceptions)
ConsoleHelper.WriteMessage(ConsoleHelper.Type.Error, $"Unhandled error: {ie.Message}\nStack Trace:\n{ie.StackTrace}", ConsoleColor.DarkRed);

return 1;
}
catch (Exception ex)
{
ConsoleHelper.WriteMessage(ConsoleHelper.Type.Error, $"Unhandled error: {ex.Message}", ConsoleColor.DarkRed);
ConsoleHelper.WriteMessage(ConsoleHelper.Type.Error, $"Unhandled error: {ex.Message}\nStack Trace:\n{ex.StackTrace}", ConsoleColor.DarkRed);
return 1;
}

return 0;
Expand Down

0 comments on commit 92ee845

Please sign in to comment.