Skip to content

Commit

Permalink
Merge pull request #9 from microsoft/add-global-exception-handler
Browse files Browse the repository at this point in the history
adding global exception handler
  • Loading branch information
bradarm authored Jul 22, 2024
2 parents b84f523 + 5a794ac commit 9c2276e
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 @@ -37,5 +37,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 9c2276e

Please sign in to comment.