Script error in MDN web extension example - javascript

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}`);
}

Related

Chrome extension - unable to catch javascript error

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.

JSON.parse() on Node.js

When I turned on the server, it stops if the function JSON.parse() failed to use. Is there any other way to get the return value of JSON.parse()? I want to throw the error when the function doesn't work. When I import data that is not in JSON format, I want to ignore the error in the function.
This can be achieved with a try...catch statement.
The try...catch statement marks a block of statements to try, and specifies a response, should an exception be thrown.
For example, the following code snippet will attempt to parse invalid JSON.
const jsonImportedData = 'not valid JSON';
try {
JSON.parse(jsonImportedData);
} catch(error) {
console.log(error);
}
If you look at the console, you'll see a log with the JSON.parse error message; however, your program will not throw an actual error since it was gracefully handled.

Asp.net: PageMethods call results in error "Type 'System.String' is not supported for deserialization of an array."

I'm try to use PageMethods to call a code behind static method, but it looks like it never gets to that method. It just fails with the error
"Type 'System.String' is not supported for deserialization of an array."
I have this working on another part of my program so I'm confused why it's broke here. I'm using a third party plugin jQuery called Chosen I wonder if that is the issue? Here is my code:
JavaScript file
$('.txtBusinessUnit').chosen().change(function ()
{
var bu = $('.txtBusinessUnit').val();
PageMethods.BUChanged(bu, onBUSuccess, onBUFailure);
});
function onBUSuccess(result) {
alert(result)
}
function onBUFailure(error) {
alert(error); //errors message listed above
}
code behind
[WebMethod]
public static string BUChanged(string BU)
{
return "it works";
}
Update: I used the Chrome debugger. It has the following error:
POST http://localhost:54234/Default.aspx/BUChanged 500 (Internal Server Error)
it errors on ScriptResource.axd with send(body) at this line
this._xmlHttpRequest.send(body);
With the following error
Failed to load resource: the server responded with a status of 500 (Internal Service Error)
Not sure what this means really..

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.

does this error affect my code.?

i m getting error in firebug console
uncaught exception: [Exception... "'Invalid save data.' when calling method: [nsIDOMEventListener::handleEvent]" nsresult: "0x8057001e (NS_ERROR_XPC_JS_THREW_STRING)" location: "JS frame :: chrome://firebug/content/spy.js :: callPageHandler :: line 744" data: no]
When i put this code
value = key+'^_^'+value;
But when i modify this code and put
value = key
the error removes.
so does this error affect my code?
I have an inkling that this might be a good place for exceptions. Exceptions allow you catch errors, which in Javascript is very handy indeed because it also allows you to execute altetrnative code if your browser doesn't support what you are trying to do.
try {
somethingThatOnlyWorksInChrome();
}
catch (ex) {
worksInEverythingElse();
}

Categories