Why does catch not return the error in this case? [duplicate]

Beginner who just got into Fetch API. I’m trying to console.log the error message that appears when someone attempts to fetch a file that doesn’t exist but it doesn’t work. It only. Can anyone please explain where I’m going wrong.

document.getElementById('button1').addEventListener('click', getText);

//getText function
function getText() {
    //fetching file that doesn't exist to produce the error
    fetch('text.text')
    .then(function(response){
        return response.text();
    })
    .then(function(data){
        console.log(data);
    })//what's supposed to happen when the error is caught
    .catch(function(error){
        console.log(error);
    })
}
%d bloggers like this: