I'm requesting some data from an API - in particular https://api.tvmaze.com/singlesearch/shows?q=Friends&embed=episodes
usually, when I get an invalid request with APIs I get it in JSON format. So I can easily send that data back (to discord). But with this API if you replace friends
with a random string it returns a page saying This api.tvmaze.com page can't be found
. How can I send this data back to the user?
I'm using NodeJS and the node-fetch
module to get the request.
fetch('https://api.tvmaze.com/singlesearch/shows?q=' + msg + '&embed=episodes') //msg is the users input
.then(function(res) {
return res.json();
}).then(function(json) {
console.log(json.network.name) // if input is friends this returns NBC
});
So I am not 100% familiar with node-fetch but I think you can check the status of the res in your .then() function.
fetch('https://api.tvmaze.com/singlesearch/shows?q=' + msg + '&embed=episodes') //msg is the users input
.then(function(res) {
if(res.status === 404) {
//HANDLE THE 404 Page Not Found
} else {
return res.json();
}
}).then(function(json) {
console.log(json.network.name) // if input is friends this returns NBC
});