Provides error handling in app.js and in controllers, supplying appropriate status codes for requests
Number of Fixes
1
Related Ticket(s)
Fixes #86
Waiting on #107 - cannot properly handle errors with DELETE until this issue is closed
Problem to Solve
Previously, we did not have a consistent way of handling errors, or in providing informative status codes about requests.
Proposed Changes
app.js has a catch-all error handler that receives next(err) from the controllers, sending the status code and error message to the client (if received from a controller) and otherwise sends 500: Internal Server Error.
For GET requests, if no data is available at the requested spot - whether all the data in a table or at a specific id - controllers return 204
We return 204 instead of 404 because there may not necessarily be data at the requested spot in the future
For POST & PUT functions, it returns 400 if the user did not send in the required parameters
Expected Behavior
A GET call to a table that does exist, but doesn't have the particular resource requested, returns 204. E.g. computers/34023 when there is no computer_id = 34023 returns 204
A POST or PUT call to a table that doesn't have the required parameters returns 400 & message with the required parameters. E.g., POSTcomputers/ with { "some": "random", "data": "man" } returns 400 and { error: "Please include: purchase_date and decomission_date" }.
Querying a URL path that doesn't exist returns 404
Description
Provides error handling in
app.js
and incontrollers
, supplying appropriate status codes for requestsNumber of Fixes
1
Related Ticket(s)
Fixes #86 Waiting on #107 - cannot properly handle errors with
DELETE
until this issue is closedProblem to Solve
Previously, we did not have a consistent way of handling errors, or in providing informative status codes about requests.
Proposed Changes
app.js
has a catch-all error handler that receivesnext(err)
from thecontrollers
, sending the status code and error message to the client (if received from acontroller
) and otherwise sends500: Internal Server Error
.GET
requests, if no data is available at the requested spot - whether all the data in a table or at a specificid
- controllers return204
204
instead of404
because there may not necessarily be data at the requested spot in the futurePOST
&PUT
functions, it returns400
if the user did not send in the required parametersExpected Behavior
GET
call to a table that does exist, but doesn't have the particular resource requested, returns204
. E.g.computers/34023
when there is nocomputer_id = 34023
returns204
POST
orPUT
call to a table that doesn't have the required parameters returns400
& message with the required parameters. E.g.,POST
computers/
with{ "some": "random", "data": "man" }
returns400
and{ error: "Please include: purchase_date and decomission_date" }
.URL
path that doesn't exist returns404
500
Steps to Test Solution
npm run db:generate
In your DB browser run:
DELETE FROM Computers
npm start
http://localhost:8080/api/v1/computers should return
204
http://localhost:8080/api/v1/computers/52 should return
204
POST
http://localhost:8080/api/v1/orders/
, this payload:Should return
400
and{ error: "Please include: customer_id and payment_type_id" }
PUT
http://localhost:8080/api/v1/orders/2
, this payload:Should return
400
and{ error: "Please include: customer_id and payment_type_id" }
Testing
[ ] There are new unit tests in this PR, and I verify that there is full coverage of all new code. [x] I certify that all existing tests pass