When comparing API's, I see a lot of different ways how error messages are returned. With the introduction of Problem Details for HTTP APIs (https://tools.ietf.org/html/rfc7807) , we finally have a standardized error payload to return when an unhandled exception occurs.
Although the standard was introduced before .NET 7, there was no out-of-the-box way to introduce the ProblemDetails spec into your ASP.NET Core application.
A solution was to use the third party Hellang.Middleware.ProblemDetails nuget package:
Starting from .NET 7 this nuget package is no longer necessary. You only need to add the following line to your service configuration:
If someone now calls your API and an exception occurs, the returned result will look like this:
We can further customize the behavior through CustomizeProblemDetails: