How to catch the network error while fetching the error in promise
<p>So I have this Picker component, and for the items, I am fetching the data from the server and mapping it when the state is changed.</p>

<pre><code>//api.js
var api = {
    getBooks() {
        var url = 'http://192.168.43.14:8080/api/books/'
        var headers = new Headers();
        headers.append('Accept', 'application/json');
        return fetch(url, headers)
            .then((res) =&gt; res.json())  
            .catch(function(err) {  
                console.log('Fetch Error :-S', err);
                throw Error(err);
            });
    }
}

//RegisterMobile.js
import api from '../utilities/api'

export default class RegisterMobile extends Component {
    constructor(props) {
        super(props)
        this.state = {
            books: [],
            book: '',
            mobile: '',
            displayError: false,
            error: 'Please provide a valid mobile number'
        }
    }

    componentWillMount() {
        api.getBooks().then((res) =&gt; {
            this.setState({
                books: res
            })
        })
    }

    render() {
        return(
            &lt;View&gt;
                &lt;View style={styles.selecBook}&gt;
                    &lt;Picker selectedValue={this.state.book} onValueChange={this.changeBook}&gt;
                        {this.state.books.map((book) =&gt; {return &lt;Picker.Item value={book.name} label={book.name} key={book.id}  /&gt;})}
                    &lt;/Picker&gt;
                &lt;/View&gt;
                {this.state.displayError ? &lt;Text style={styles.error}&gt;{this.state.error}&lt;/Text&gt; : null}
            &lt;/View&gt;
        )
    }
}
</code></pre>

<p>This is working fine. I get the list of items and when I click on the Picker, I can select the items. What I want to do is, if there was any error while fetching the data (eg. server is down), I would like to get that error and display it as error (which would be just changing the state of <code>error</code> and <code>displayError</code>). But I don't know how to get the error if there was one to set it in the state of the error.</p>