Chrome extension - unable to catch javascript error - javascript

I am trying to catch all JavaScript errors from a specific website via a chrome extension. I am very inexperienced with javascript and for some odd reason my error handler isnt catching all of the errors, most importantly, the one on line 191 "Load timeout for modules":
https://hastebin.com/atuwiboqec.js
The error "script error" on line 507 comes through just fine.
This is my content.js file that I have under "content_scripts" in the manifest, it runs at document_start:
var script=document.createElement("script");
script.src=chrome.runtime.getURL("myscript.js");
script.async=false;
document.documentElement.appendChild(script);
This is my myscript.js file:
window.addEventListener("error", handleException, false);
window.addEventListener("unhandledrejection", handleException, false);
function handleException(I_sMsg) {
console.log("Error0 occured: " + I_sMsg.message);
return cancelEvent(I_sMsg);
};
window.onerror = function ErrorHandler(errorMsg, url, lineNumber) {
console.log("Error1 occured: " + errorMsg);
return false;
};
window.addEventListener("timeout", function(e) {
console.log("Error2 occured: " + e.error.message);
return false;
});
When the error "script error" occurs, both Error0 and Error1 messages get printed. When the error "load timeout for modules" occurs, nothing happens even though the error shows up as "Uncaught Error: Load timeout for modules" in the chrome console, in red, with log level "error". This indicates that the error is being thrown:
error in console image
How can I catch that error, or even better, all errors from that site?
P.S I am unsure of how to trigger that load timeout error. It only happens every now and then. I believe a slow network is the cause.
EDIT: Updated the url domain to a backup one as it wasnt resolving.

Related

How to handle page crashed exception in node.js puppeteer?

Occasionally, when I run my puppeteer script, I’ll get a “page crashed” error. It’s most likely not a software error, but something to do with the proxy IPs I’m using/webpages I’m scraping. So all I’m really looking to do is just catch the error, close the current page, and open up a new page.
However, my try catch block doesn’t seem to be working. My code is structured like this:
try {
//puppeteer code
}
catch(e) {
console.log(‘caught error ‘ + e);
}
But when the page crashed error happens, I see “unhandled promise rejection error” instead of my console.log message. What’s going on here?
From here:
page.on('error', msg => {
console.error(...);
throw msg ;
});
And this is also advised to globally catch all uncaught errors in your code:
process.on('unhandledRejection', error => {
console.error(...);
throw error;
});

Error event listener not handling load resource errors

I looked everywhere but I couldn't find something that helped. This has been really annoying me because I actually tested what I was doing a few weeks ago and it worked then.
So here's my code:
var error_handler = function(e) {
console.error(e);
e.preventDefault();
}
window.addEventListener('error', error_handler);
And that works for handling errors, but it's not handling errors like this:
Failed to load resource: the server responded with a status of 404 ()
when I wrote the code before, it did pick up those errors. Any idea what I'm doing wrong here?
When a resource (such as an <img> or <script>) fails to load, an error event using interface Event is fired at the element that initiated the load, and the onerror() handler on the element is invoked. These error events do not bubble up to window, but can be handled with a EventTarget.addEventListener configured with useCapture set to true.
For more details refer this documentation
Actually, error event does work for resource loading errors, you just need to set useCaputure to true
window.addEventListener("error", (event) => {
console.log('error: ', event);
}, true);
You will not get the status code or text of the network response, but you will have an event that shows you the target that throws the error.
For example:
> error: Event {isTrusted: true, type: 'error', target: img, currentTarget: Window, eventPhase: 1, …}
Loading an external file is not a JavaScript error so it is not going to be caught unless the error comes from the script file from trying to be executed. You can add an error event to the script tag, but you are not going to get the error message from it.
function handleError(evt){
console.log(evt);
}
var scr = document.createElement("script");
scr.src="error.js";
scr.onerror = handleError
document.body.appendChild(scr);

Script error in MDN web extension example

I am trying to run the MDB example extension at https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Your_second_WebExtension and it always fails with the message:
Failed to execute beastify content script unexpected token: ':'
The other message in the console says. scriptElement:88:13
I thought the error message comes from the : in the line but even removing it still gives the message, so there must be something passed in the string error.message that is causing the error.
The whole project is at https://github.com/mdn/webextensions-examples/tree/master/beastify and the error is from choose_beast.js line 88.
From what I see the ${error.message} contains some executable string that has a syntax error.
function reportExecuteScriptError(error) {
document.querySelector("#popup-content").classList.add("hidden");
document.querySelector("#error-content").classList.remove("hidden");
console.error(`Failed to execute beastify content script: ${error.message}`);
}

Dash.js - Trapping 404 errors from MPEG-DASH player

I've using the Dash.js player to play MPEG-DASH videos. The videos are pulled from a server. Every now and then there will be 404 errors due to server issues, I would like to retry the stream in the background by detecting the 404 error and acting accordingly.
The problem is I cannot catch the error, it's thrown from the line
req.send();
Which is in a file called FragmentLoader.js.
I've tried the following error handling:
window.addEventListener('error', function(e) {
console.log("Item: " + e.message);
}, true);
var oReq = new XMLHttpRequest();
oReq.addEventListener("error", function (e) {
console.log("xml item: " + e.message);
}, true);
$(document).ajaxError(function (event, xhr, ajaxOptions, errorThrown) {
alert("ajax erorr");
});
However none of these conditions catch the error. Is there any way to catch these errors thrown from the dash.js player?
Dash.js has internal retry logic on a 404 - it will retry 3 times (so a total of 4 attempts) before giving up. There's some discussion about improving this behaviour further such as trying the other available representations, but that isn't there yet.
However, this depends on the 404 being detected by the page. There are some XHR errors that are completely silent in terms of what JavaScript can see, even though an error is logged to the console on the exact line of req.send(), which is behaviour I've seen here: https://github.com/Dash-Industry-Forum/dash.js/issues/1209
If the request is indeed throwing a 404 that Dash.js' error handling has handled, retried, and then gave up, then you can bind to its error event:
var url = "http://dash.edgesuite.net/envivio/Envivio-dash2/manifest.mpd";
var player = dashjs.MediaPlayer().create();
player.initialize(document.querySelector("#videoPlayer"), url, true);
player.on('error', function(e) {
if (e.error === 'download') {
// dash.js gave up loading something
// e.event.id will tell you what it failed to load (mpd, segment...)
// and e.event.url will have the URL that failed
}
});
its been a bit since i've been in that code, but if memory serves, you should be able to determine it is a 404 by checking the status property of the error
var oReq = new XMLHttpRequest();
oReq.addEventListener("error", function (e) {
console.log("xml item: " + e.status);
}, true);

Get the javascript errors that are printed to browser console

Is there a way to catch the errors that are written to the console (e.g unrecognized token) in code? I want to write them to a log of some kind.
You can listen to the window's onerror event.
window.onerror = function(errorMsg, url, lineNumber, charNumber, errObj) {
alert(errObj.message);
};
abc();
This will afaik only be emitted if there is run time errors in loaded files.
You should be able to return false to prevent the original error from showing.

Categories