I am writing weather application in JavaScript and as you can guess there are lots of API requests. So here I make a request to API which then will return me image of the city. City is coming from user input.
async getCityImage(city) {
console.log(city, 'getCityImage');
await fetch(`https://api.teleport.org/api/urban_areas/slug:${city}/images/`)
.then(res => res.json())
.then((res) => {
this.imageUrl = res.photos[0].image.mobile;
});
}
}
The problem is that user input may be inappropriate and of course API would return an error like this
> GET https://api.teleport.org/api/urban_areas/slug:munchen/images/ 404
(Not Found)
For example there are some cities which names are separated by hyphen
cansas-city, guatemala-city etc...
So I would like to handle errors so that error doesn't affect to UI, but make another request like this and then return the answer `
GET https://api.teleport.org/api/urban_areas/slug:${city}-city/images/
I have tried to make it happen with nesting requests inside then, but it doesn't work