Any web API project is not an application in itself. It is built so that popular frontends, either web or desktop, can consume it. It becomes essential for web APIs to handle any unexpected events (unhandled exceptions) or any business exception gracefully.
If unexpected events (unhandled or business-related) occur, then they should be logged and the appropriate response should be sent back so that clients can be made aware of it.
To achieve this, we require the following classes:
- WebAPIError: It acts as a response object to be used by clients.
- WebAPIException: It is a custom exception class for business or unhandled exceptions. It contains exception details, along with the status code.
- WebAPIExceptionFilter: It contains an exception attribute to be used as an attribute on the controller or actions.
Create a ErrorHandlers folder and...