I'm building something that includes javascripts on the fly asynchronously, which works, but I'm looking to improve upon the error detection (so all the errors don't just appear to come from some line near the AJAX call that pulls them down.
If I'm using eval to evaluate a multiline javascript file, is there any way to trace which line an error occurs on?
By keeping references to the variables I need when including, I have no problem determining which file the errors occurs in. My problem is determining which line the error occurs in.
Example:
try {
eval("var valid_statement = 7; \n invalid_statement())))");
} catch(e) {
var err = new Error();
err.message = 'Error in Evald Script: ' + e.message;
err.lineNumber = ???
throw err;
}
How can I tell that the error occurred in the second line there? Specifically I'm interested in doing this in Firefox.
I know that error objects have e.stack
in Mozilla browsers, but the output doesn't seem to take into account newlines properly.
Catching exceptions To catch an exception, use eval() . eval() parses, compiles, and evaluates a block of code at compile time and catches any exceptions that are raised at runtime. The exception is placed in the global variable $@ .
eval() is evil if running on the server using input submitted by a client that was not created by the developer or that was not sanitized by the developer. eval() is not evil if running on the client, even if using unsanitized input crafted by the client.
page.$$eval(selector, pageFunction[, ...args]) This method runs Array. from(document. querySelectorAll(selector)) within the page and passes it as the first argument to pageFunction . If pageFunction returns a Promise, then page. $$eval would wait for the promise to resolve and return its value.
Malicious code : invoking eval can crash a computer. For example: if you use eval server-side and a mischievous user decides to use an infinite loop as their username. Terribly slow : the JavaScript language is designed to use the full gamut of JavaScript types (numbers, functions, objects, etc)… Not just strings!
eval
is on.So something like...
try {
eval('var valid_statement = 7; \n invalid_statement())))');
} catch(e) {
var err = e.constructor('Error in Evaled Script: ' + e.message);
// +3 because `err` has the line number of the `eval` line plus two.
err.lineNumber = e.lineNumber - err.lineNumber + 3;
throw err;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With