Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Explaining an error

Reply
Thread Tools

Explaining an error

 
 
Nicolas George
Guest
Posts: n/a
 
      01-09-2010
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.
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-09-2010
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$(E-Mail Removed)> (2004)
 
Reply With Quote
 
 
 
 
Nicolas George
Guest
Posts: n/a
 
      01-10-2010
Thomas 'PointedEars' Lahn wrote in message
<(E-Mail Removed)>:
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Explaining DVD to a newbie? John DVD Video 23 03-07-2006 11:21 AM
explaining web standards to clients Jeff Fritz HTML 88 10-22-2005 03:24 AM
help in explaining binary tree Jerry Khoo C++ 4 04-05-2004 05:33 PM
Explaining how a (Mind) program works Arthur T. Murray Java 1 09-26-2003 11:38 PM
Explaining how a (Mind) program works (Was: Re: Learning...) Arthur T. Murray C++ 2 09-26-2003 06:28 PM



Advertisments