fix: catch exceptions in handlers directly

Previously, any exceptions that were occuring in a hander's action were being swalled due to being off on another thread. Now, those exceptions are caught and printed out.
This commit is contained in:
Andrew Welker
2025-10-15 14:03:17 -05:00
parent 6cb98e12fa
commit 5c35a3be45

View File

@@ -2337,10 +2337,33 @@ namespace PepperDash.Essentials
foreach (var handler in handlers) foreach (var handler in handlers)
{ {
Task.Run( Task.Run(async () =>
() => {
handler.Action(message.Type, message.ClientId, message.Content) try
); {
handler.Action(message.Type, message.ClientId, message.Content);
}
catch (Exception ex)
{
this.LogError(
"Exception in handler for message type {type}, ClientId {clientId}",
message.Type,
message.ClientId
);
this.LogDebug(ex, "Stack Trace: ");
}
}).ContinueWith(task =>
{
if (task.IsFaulted && task.Exception != null)
{
this.LogError(
"Unhandled exception in Task for message type {type}, ClientId {clientId}",
message.Type,
message.ClientId
);
this.LogDebug(task.Exception.GetBaseException(), "Stack Trace: ");
}
}, TaskContinuationOptions.OnlyOnFaulted);
} }
break; break;