Managing exceptions
In this recipe, you will learn how to manage exceptions with Web API.
Usually, we don't want to lose the cause of an exception. So, we should persist and maintain all the exception's logs. Exception logs can be huge in terms of storage capacity needed to persist them.
We can log all the exceptions in files, or a database table (such as MSSQL, Oracle, or MySql), or document stores (such as MongoDb (https://www.mongodb.com/), or Azure CosmosDb (https://azure.microsoft.com/en-us/services/cosmos-db/)).
The logged exception would contain at least:
- A descriptive message
- The exception message
- The exception .NET type
- The stack trace
We generally send only the descriptive message to the client, and log other information about the exception.
With Web API 2, before ASP.NET Core, the HttpError
class sent a structured error to the client.
HttpError
was traditionally used by Web API to serve up error information to the client in a (kind of) standardized way. It had some interesting properties...