Explaining an error

Discussion in 'Javascript' started by Nicolas George, Jan 9, 2010.

  1. 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.
    Nicolas George, Jan 9, 2010
    #1
    1. Advertising

  2. 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$> (2004)
    Thomas 'PointedEars' Lahn, Jan 9, 2010
    #2
    1. Advertising

  3. Thomas 'PointedEars' Lahn wrote in message
    <>:
    > 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.
    Nicolas George, Jan 10, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arthur T. Murray

    Explaining how a (Mind) program works

    Arthur T. Murray, Sep 26, 2003, in forum: Java
    Replies:
    1
    Views:
    370
    Kent Paul Dolan
    Sep 27, 2003
  2. Jeff  Fritz
    Replies:
    88
    Views:
    1,546
    Jonathan N. Little
    Oct 22, 2005
  3. Arthur T. Murray
    Replies:
    2
    Views:
    388
    E. Robert Tisdale
    Sep 26, 2003
  4. Jerry Khoo

    help in explaining binary tree

    Jerry Khoo, Apr 5, 2004, in forum: C++
    Replies:
    4
    Views:
    528
    Kevin Goodsell
    Apr 5, 2004
  5. AC [MVP MCMS]

    Understanding/explaining WebZones....

    AC [MVP MCMS], May 31, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    421
    AC [MVP MCMS]
    May 31, 2006
Loading...

Share This Page