# On an error occurring

**URL:** <https://community.fliplet.com/t/on-an-error-occurring/366>\
**Category:** Code Library\
**Created:** [July 1, 2022, 1:55pm UTC](https://community.fliplet.com/t/on-an-error-occurring/366 "2022-07-01T13:55:43Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mel](https://dub1.discourse-cdn.com/flex017/user_avatar/community.fliplet.com/mel/32/3_2.png) [@Mel](https://community.fliplet.com/u/Mel)\
**Post date:** [July 1, 2022, 1:55pm UTC](https://community.fliplet.com/t/on-an-error-occurring/366/1 "2022-07-01T13:55:43Z")

</div>

### Do something when an error occurs

_ **PRE-REQUISITES** _

For the following code examples to work, you will need to add Fliplet’s Data Sources API to your app. To add it follow the steps [referenced here](https://code.fliplet.com/add-fliplets-data-sources-api-to-your-app/).

There are specific instances where a Fliplet component will fire an error message (such as when a form fails to submit). However, to apply this more generally we can use a JavaScript promise. A promise tries to do something but if it fails a `catch()` function is called.

In the example below we attempt to connect with an external data source. There is a chance we cannot connect so we will structure our code as a promise. If we fail to connect, the code below the catch block will be run. In this case, we will show the error on the screen.

**JavaScript**

```javascript
Fliplet.DataSources.connect(0)
  .then(function (connection) {
    return connection.find();
  })
  .catch(function (error) {
    // Code to be run if there is an error
    Fliplet.UI.Toast.error(error, 'Error loading data');
  });

```

We can `catch()` the error if it fails to connect to the data source, and in the example above we show the error message in the app using the [Fliplet toast](https://community.fliplet.com/t/generate-a-toast-popup/355).
