Skip to content

Commit

Permalink
Merge pull request #7 from microsoft/add-global-exception-handler
Browse files Browse the repository at this point in the history
add global exception handler
  • Loading branch information
bradarm committed Jul 22, 2024
2 parents c683f81 + a70389f commit 29b9155
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,18 @@ public static void Main(string[] args) {
});
});
app.Run();

// Add a middleware to catch exceptions and stop the host gracefully
app.Use(async (context, next) => {
try {
await next.Invoke();
} catch (Exception ex) {
Console.Error.WriteLine($"Triggering shutdown due to exception caught in global exception handler. Error: {ex.Message}. Stack Trace: {ex.StackTrace}");
// Stop the host gracefully so it triggers the pod to error
var lifetime = context.RequestServices.GetService<IHostApplicationLifetime>();
lifetime?.StopApplication();
}
});
}
}

0 comments on commit 29b9155

Please sign in to comment.