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