Step 5
Add the file ApiError.js
to model
folder, with the following content:
class ApiError extends Error {
constructor(status, message) {
super(message);
this.status = status;
}
}
module.exports = ApiError;
We will use this class for error handling.
In APIs that conform to HTTP, the error status is a "code" (number) returned to the client that signals the success/failure of their request. Here are the common status codes and their meaning:
Status | Meaning |
---|---|
200 (OK) | This is the standard response for successful HTTP requests. |
201 (CREATED) | This is the standard response for an HTTP request that resulted in an item being successfully created. |
204 (NO CONTENT) | This is the standard response for successful HTTP requests, where nothing is being returned in the response body. |
400 (BAD REQUEST) | The request cannot be processed because of bad request syntax, excessive size, or another client error. |
403 (FORBIDDEN) | The client does not have permission to access this resource. |
404 (NOT FOUND) | The resource could not be found at this time. It is possible it was deleted, or does not exist yet. |
500 (INTERNAL SERVER ERROR) | The generic answer for an unexpected failure if there is no more specific information available. |
Save and commit the changes.