Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find where a syntax error occurs when new Function() fails

To shortcut a long comment section on "don't use new Function" and/or "eval is evil", this question is about how to access, if possible, error information that is related to a new Function() constructor failing. It's mostly a question to discover a limit in what the browser will let me do when trying to exploit JavaScript to the extent that the spec and standard browser implementations allow. So with that disclaimer in place:

When evaluating code through a new Function() call, is there a way to find out where in the function's content a syntax error occurs, if illegal-syntax code is being evaluated? i.e.:

try {
  var generator = new Function(input);
  try {
    generator();
  }
  catch (runtimeError) {
    console.error("legal code; unforeseen result: ", runtimeError);
  }
}
catch (syntaxError) {
  console.error("illegal code; syntax errors: ", syntaxError);
}

When the building of the generator fails, is there a way to find out (from the browser, not using jslint or another external library) what the error was or where it occurred?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError/prototype mentions that a SyntaxError has a filename and linenumber property, but these are undefined for dynamic code evaluated through a new Function() constructor from what I can tell, so relying on the error object itself seems not to be an option. Are there alternative ways to introduce the code to the browser so that the code, once we know it has syntax errors from a failing new Function call, can be used to find out where the problem is according to the JS engine used?

(Of course, if the goal was to simply find syntax errors, jslint as a preprocess step would be the go-to solution, but I'm more interested in whether or not browsers can in some way be made to report this information, even if in limited form like "there is SOME error on line/char ...")

like image 725
Mike 'Pomax' Kamermans Avatar asked Sep 04 '13 21:09

Mike 'Pomax' Kamermans


People also ask

How do you fix a syntax error?

Fix Syntax Error Caused By Editing a Theme File Improperly php file. Edit the file and correct the error. Again, the syntax error code should display the line number. If the problem occurred when you pasted a code snippet into the file, delete your edits to restore the file to its stable version.

How do you fix a syntax error in Python?

You can clear up this invalid syntax in Python by switching out the semicolon for a colon. Here, once again, the error message is very helpful in telling you exactly what is wrong with the line.

What is an example of syntax error?

Syntax errors are mistakes in using the language. Examples of syntax errors are missing a comma or a quotation mark, or misspelling a word.


1 Answers

afaik impossible to find out where it occured. but you may want to see Exception.message to fetch information what the error was.

example: http://jsbin.com/IRoDiJIV/1/watch?js

like image 181
floww Avatar answered Oct 02 '22 22:10

floww