I need to detect the blocked network requests and trigger conditionally specific logic.
Assuming the blocked requests are followed by error messages thrown into the console, I tried to intercept both console.error
messages and catch window.onerror
events, but at no avail.
For a script, triggering network request (which is getting blocked by CORS-policy and throw sample errors):
throw.js
(function () {
fetch("https://checkip.amazonaws.com");
})();
I have a listener:
listen.js
(function () {
const _consoleError = console.error;
window.consoleErrors = [];
window.errors = [];
console.error = (str) => {
window.consoleErrors.push(str);
_consoleError(str);
};
window.onerror = (errorMsg) => {
window.errors.push(errorMsg);
};
})();
And despite I see the error in the console:
throw.js:2 GET https://checkip.amazonaws.com/ net::ERR_FAILED
Both arrays (window.errors
and window.consoleErrors
) are empty.
So, my question is: how do I get the error message ('GET https://checkip.amazonaws.com/ net::ERR_FAILED'
) caught and processed? The ideas to tackle the initial task in some alternative way are also welcome.
s