Use IExceptionHandler to handle errors in AspNet Core
No matter how carefully you design your web pages and components there is always a possibility that your application throws an error at runtime. To trap and handle such unforeseen circumstances you can use IExceptionHandler interface introduced in ASP.NET Core 8.
https://www.binaryintellect.net/articles/3f87c933-2e21-4df4-9c26-8f350afc68da.aspx