I'm using XMLHttpRequest
to consume APIs on the background. Everything's working fine! My only problem is that when there's no internet, it prints an error message on the console even when it's handled by XMLHttpRequest.onerror
I'm making a PWA that can work offline... it will try to connect but if it failed, that's okay...
Simplified experimental scenarios:
Running the following code shows an error when the internet is disconnected (after installing the app):
let x = new XMLHttpRequest();
x.onload = //do something...
x.onerror = //do another thing...
x.open("GET", "/someresource");
x.send();
And even the following still shows an error when there's no internet:
try {
let x = new XMLHttpRequest();
x.onerror = (e) => {
console.log(
"Don't worry about anything! \
This error was handled successfully"
);
e.preventDefault();
return true;
};
x.open("GET", "/some-resource");
x.send();
} catch (e) {
console.log("It's all under control");
}
Screenshot of the error that should not happen
Which triggers some OCD tendencies for me! I can't sleep before this error is handled...