From: Nicolas George on
Hi.

I have the following piece of code:

var code = <some code from the user>;
eval(code);

If there is a syntax error in the evaluated code, the "error console" of
Firefox is able to pinpoint the exact location of the error. Firebug does
that also.

I would like to display the error in the web page.

If I catch the SyntaxError exception in the eval and print it as a string, I
get the helpful message ("missing ; before statement" for example), but not
the exact location. The stack property of the exception is not accurate
enough.

I also tried to add chrome://global/content/console.xul in an iframe, but
"security reasons" prevent it.

Does anyone have an idea to make the error message more helpful? A
Firefox-specific solution is ok for me.

Thanks.
From: Thomas 'PointedEars' Lahn on
Nicolas George wrote:

> I have the following piece of code:
>
> var code = <some code from the user>;
> eval(code);

What do you need this for?

> If there is a syntax error in the evaluated code, the "error console" of
> Firefox is able to pinpoint the exact location of the error. Firebug does
> that also.

Firebug gets only what SpiderMonkey/TraceMonkey tells it. As it can tell
you.

> I would like to display the error in the web page.
>
> If I catch the SyntaxError exception in the eval and print it as a
> string, I get the helpful message ("missing ; before statement" for
> example), but not the exact location. The stack property of the
> exception is not accurate enough.
>
> I also tried to add chrome://global/content/console.xul in an iframe, but
> "security reasons" prevent it.
>
> Does anyone have an idea to make the error message more helpful? A
> Firefox-specific solution is ok for me.

You could have been a bit more precise about your greater objective and
your requirements for a solution.

Anyhow, after running this quick hack in Firebug (1.5.0b9) on a randomly
chosen Web site:

var f = document.createElement("form");
f.action = "";
f.style.position = "fixed";
f.style.left = f.style.top = "0";
f.style.width = "200px";
f.style.height = "100px";
f.onsubmit = function(ev) {
try
{
eval(this.elements[0].value);
}
catch (e)
{
console.log(e);
}

ev.preventDefault();
return false;
};

var inp = document.createElement("input");
inp.style.position = "relative";
inp.style.width = "100%";

f.appendChild(inp);
document.body.appendChild(f);

-- and entering "+" (a syntax error) in the new form input, I get Firebug
to show

SyntaxError: syntax error { message="syntax error", more...}

which, upon clicking, displays (trimmed to fit into 76 columns)

| fileName "http://localhost/scripts/test/es-matrix/#o"
| lineNumber 84
| message "syntax error"
| name "SyntaxError"
| [-] stack "eval("+")@:0
| ([object Event])@http://localhost/scri.../es-matrix/#o:84
| "

which is, of course, a listing of the known properties of the Error
instance.

What exactly are you missing there?


PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7(a)news.demon.co.uk> (2004)
From: Nicolas George on
Thomas 'PointedEars' Lahn wrote in message
<1519260.YKUYFuaPT4(a)PointedEars.de>:
> What do you need this for?

The purpose is a simple interactive programming environment.

> Firebug gets only what SpiderMonkey/TraceMonkey tells it. As it can tell
> you.

Yes. And the interesting thing is that it gets the information. I would like
to be able to find it too.

> -- and entering "+" (a syntax error) in the new form input, I get Firebug
> to show
>
> SyntaxError: syntax error { message="syntax error", more...}
>
> which, upon clicking, displays (trimmed to fit into 76 columns)
>
> | fileName "http://localhost/scripts/test/es-matrix/#o"
> | lineNumber 84
> | message "syntax error"
> | name "SyntaxError"
> | [-] stack "eval("+")@:0
> | ([object Event])@http://localhost/scri.../es-matrix/#o:84
> | "
>
> which is, of course, a listing of the known properties of the Error
> instance.
>
> What exactly are you missing there?

If you change your code to either:

eval("a=42;\nb=*42;");

or

eval("a=*42;\nb=42;");

Firebug or the Error Console display respectively the exact line of the
error, "b=*42;" or "a=*42;" respectively. The error console even puts a
green arrow just below the offending "*".

On the other hand, if you examine the exception object, you see it has
exactly the same properties. The only difference is the stack string, which
copies the difference of the source code:

[-] stack "eval("a=42;\n*b=42;")@:0\n ...

[-] stack "eval("a=*42;\nb=42;")@:0\n ...

But nothing tells me where exactly the error is. Of course, on such a
minimalistic example, the error is obvious, but that is not the question.