07 - Error Handling
In this module we'll write code that would handle errors if something in our server were to go wrong.
Overview
What about the idea of a network problem as we're making our request? We make the request, the network drops, and the response never comes. What if something is wrong with our server code? We're going to need to create a function that handles such errors.
The Code
Go into the testcontroller.js
file and add the following method. Add it to the bottom of the file, above the export statement.
Analysis
The addition that we've made here is an error function. If the
create()
function returns an error, it will be picked up by thecreateError()
method. That method will then send back a500
error with a message.
Summary of the Flow
In this module, the following flow is happening:
1. We make a POST request with Postman.
2. body-parser
breaks the request into JSON.
3. The router sends the request to the testcontroller
.
4. The controller with the /seven
endpoint is called.
5. The req.body.testdata.item
is captured in the testData
variable.
6. We then use the Sequelize create()
method to create the object to be sent to the DB.
7. The object is sent and Postgres stores it.
8. After the data is stored, we fire the then()
method, which returns a Promise.
9. We call a method that takes in a parameter called testdata
. It holds the data for the response.
10. The method sends the data back as JSON this time, and the response goes to Postman.
11. We also have an error for cases when our server throws an error.
Last updated