chicken-sloths / bangazon-api-sprint1

API for a mock Amazon + Etsy platform providing developers access to the company's employee & product data
0 stars 0 forks source link

Fixes #86: Error handling & status codes #125

Closed DavidLarsKetch closed 6 years ago

DavidLarsKetch commented 6 years ago

Description

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

  1. 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.
  2. 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
    1. We return 204 instead of 404 because there may not necessarily be data at the requested spot in the future
  3. For POST & PUT functions, it returns 400 if the user did not send in the required parameters

Expected Behavior

  1. 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
  2. A POST or PUT call to a table that doesn't have the required parameters returns 400 & message with the required parameters. E.g., POST computers/ with { "some": "random", "data": "man" } returns 400 and { error: "Please include: purchase_date and decomission_date" }.
  3. Querying a URL path that doesn't exist returns 404
  4. Internal Server Errors return 500

Steps to Test Solution

  1. npm run db:generate

  2. In your DB browser run: DELETE FROM Computers

  3. npm start

  4. http://localhost:8080/api/v1/computers should return 204

  5. http://localhost:8080/api/v1/computers/52 should return 204

  6. POST http://localhost:8080/api/v1/orders/, this payload:

    {
    "something": "random"
    }

    Should return 400 and { error: "Please include: customer_id and payment_type_id" }

  7. PUT http://localhost:8080/api/v1/orders/2, this payload:

    {
    "something": "random"
    }

    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

jordan-castelloe commented 6 years ago

Testing again!