I have a simple API call that sets the state of a list array with its response. I was wondering how I would go about implement a try/catch or error message if there is a bad search (i.e like a typo) or if the array is not set with the response. The code snippet is below:
componentDidMount() {
this.search('https://itunes.apple.com/search?term=modern_baseball');
}
search(URL) {
return $.ajax({
type: 'GET',
dataType: 'json',
url: URL,
success: function (response) {
this.showResults(response);
}.bind(this),
error: function() {
alert("Error handling request");
}
});
}
showResults(response) {
console.log(response);
this.setState({
searchResults: response.results,
moveResults: []
});
}