# 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.

```javascript
/*********************
 * Route 7: Handle errors
 **********************/
router.post('/seven', function (req, res) {
  var testData = req.body.testdata.item;

  TestModel
    .create({
      testdata: testData
    })
    .then(
      function createSuccess(testdata) {
        res.json({
          testdata: testdata
        });

      },
      function createError(err) { //1
        res.send(500, err.message);
      }
    );
});

module.exports = router;
```

## Analysis

1. The addition that we've made here is an error function. If the `create()` function returns an error, it will be picked up by the `createError()` method. That method will then send back a `500` error with a message.&#x20;

## Summary of the Flow

In this module, the following flow is happening:&#x20;

1\. We make a POST request with Postman.&#x20;

2\. `body-parser` breaks the request into JSON.&#x20;

3\. The router sends the request to the `testcontroller`.&#x20;

4\. The controller with the `/seven` endpoint is called.&#x20;

5\. The `req.body.testdata.item` is captured in the `testData` variable.&#x20;

6\. We then use the Sequelize `create()` method to create the object to be sent to the DB.&#x20;

7\. The object is sent and Postgres stores it.&#x20;

8\. After the data is stored, we fire the `then()` method, which returns a Promise.&#x20;

9\. We call a method that takes in a parameter called `testdata`. It holds the data for the response.

&#x20;10\. The method sends the data back as JSON this time, and the response goes to Postman.

&#x20;11\. We also have an error for cases when our server throws an error.
