I\'m developing a small Chrome extension for personal use on a very specific case (website automation) but I\'ve a problem. How can I catch a network error on a background s
With help of the the user @Xan, I managed to come up with this:
function extractStatus(line) {
var match = line.match(/[^ ]* (\d{3}) (.*)/);
if (match)
return {code: match[1], message: match[2]};
else
return undefined;
}
chrome.tabs.query({active: true, lastFocusedWindow: true }, function(tabsArray) {
tab = tabsArray[0];
scope = {urls: ["https://example.com/*"], tabId: tab.id};
console.log("Listening for errors...");
// ::net errors
chrome.webRequest.onErrorOccurred.addListener(handleNetworkError, scope);
// HTTP errors
chrome.webRequest.onHeadersReceived.addListener(function(details) {
var status = extractStatus(details.statusLine);
if (!status)
return;
if (status.code.charAt(0) == '5' || status.code.charAt(0) == '4')
handleNetworkError();
}, scope);
});