dimanche 20 juin 2021

fetching from a URL shows an error in console even after catching it?

Suppose, I have this function with a wrong 'uri' string provided to fetch:

function imgChanger(uri) {
    fetch('uri', { mode: 'cors' })//WRONG 'uri' to demonstrate that catch doesn't prevent console's red error
        .then(function (response) {
            if (!response.ok) {
                throw new Error(response.statusText);
            }
            return response.json();
        }).then(function (response) {
            img.src = response.data.images.original.url;
        }).catch((err)=>{
            console.log(err);
        });
}

Despite handling the error, I can see a 404 message in the console:

enter image description here

I have two questions regarding this: why is it being shown despite catching it and how do I handle it?




Aucun commentaire:

Enregistrer un commentaire