Error handling
Clients will make mistakes or send invalid data when sending requests, and our RESTful API needs to catch them and inform the client about what went wrong to enable the client to anticipate accordingly. The client-server communication for a DRF-based RESTful API in case of an error generally covers these steps:
- The client sends a request to an endpoint.
- The RESTful API at the endpoint parses the request.
- The URL mechanism and the serializer in the RESTful API validate the request and catch an error.
- The RESTful API sends an error message and a corresponding HTTP status code back to the client.
- The client parses the request.
- The client signals the error and catches it with alternate processing.
To implement the RESTful API part in this process, we need to handle possible errors, which falls apart in the following areas:
- Handling wrong formatted requests, such as a missing data object, when creating new data through a
POST
request...