I am learning fetch API . I am trying to display a some text content from text file in my div
element . I purposely misspelt my file name so to check if error is handled or not .Here is my update functio to display
function updateDisplay() {
const path = "file.txt";
fetch(path).then((response)=>{
response.text().then((text)=>{
mydiv.textContent =text;
})
}).catch((error)=>{
console.log("there is error in fetching" + error);
});
}
when file name is correct data is displayed correctly as expected. But when provide nonexistent file path the control is not reaching to catch block. On console i see
GET http://127.0.0.1/javascript/files.txt" 404 (Not Found)
But Biggest problem is content updated in my div element ,its all raw html response code.
I tried try catch around entire function but still the catch block is not triggering Tried multiple browser. same error.