Line Numbers on Exceptions?

17 views
Skip to first unread message

sk

unread,
May 26, 2010, 12:30:19 AM5/26/10
to Narwhal and Jack
Given:

exports.app = function(env) {
throw new Error("an error occurred");
return {
status : 200,
headers : {"Content-Type":"text/plain"},
body : ["test"]
};
};

How can I get the line number and file name of the exception? It's
especially critical for nested files and modules.

Currently it just prints:

Error: an error occurred

and the fileName and lineNumber properties of the exception object
(inside ShowExceptions middleware) are null.

RhinoExceptions would have a stack trace, but what about exceptions in
my own js?

sk

unread,
May 26, 2010, 1:01:57 AM5/26/10
to Narwhal and Jack
Maybe it's just that I don't have Rhino configured to emit stack
traces? Because I don't get stack traces even when I deliberately
crash in java.

Where is the Rhino config that comes bundled w/ narwhal?

Thanks a lot.

sk

Hannes Wallnoefer

unread,
May 26, 2010, 4:01:13 AM5/26/10
to narw...@googlegroups.com
2010/5/26 sk <doyouun...@gmail.com>:

Rhino by default follows the ES spec maybe to closely, which says
"Error instances have no special properties beyond those inherited
from the Error prototype object". To get fileName and lineNumber
properties in error objects without passing them to the constructor,
you have to set the Context.FEATURE_LOCATION_INFORMATION_IN_ERROR
property when you enter a Rhino context.

http://www.mozilla.org/rhino/apidocs/org/mozilla/javascript/Context.html#FEATURE_LOCATION_INFORMATION_IN_ERROR

With a recent Rhino snapshot this also gives you a "stack" property
containing the JavaScript stack trace like the one on the bottom of
this page:

http://ringojs.org/demo/logging?error=1

Hannes

> --
> You received this message because you are subscribed to the Google Groups "Narwhal and Jack" group.
> To post to this group, send email to narw...@googlegroups.com.
> To unsubscribe from this group, send email to narwhaljs+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/narwhaljs?hl=en.
>
>

sk

unread,
May 26, 2010, 1:01:28 PM5/26/10
to Narwhal and Jack
Thanks a lot Hannes.

Does anybody know where this config gets loaded from jack?

I realize I can probably set it in init code as a hack, but it would
be nice not to.

Thanks.

On May 26, 1:01 am, Hannes Wallnoefer <han...@helma.at> wrote:
> 2010/5/26 sk <doyouunderst...@gmail.com>:


>
>
>
>
>
> > Given:
>
> > exports.app =  function(env) {
> >        throw new Error("an error occurred");
> >        return {
> >          status : 200,
> >          headers : {"Content-Type":"text/plain"},
> >          body : ["test"]
> >        };
> > };
>
> > How can I get the line number and file name of the exception?  It's
> > especially critical for nested files and modules.
>
> > Currently it just prints:
>
> > Error: an error occurred
>
> > and the fileName and lineNumber properties of the exception object
> > (inside ShowExceptions middleware) are null.
>
> > RhinoExceptions would have a stack trace, but what about exceptions in
> > my own js?
>
> Rhino by default follows the ES spec maybe to closely, which says
> "Error instances have no special properties beyond those inherited
> from the Error prototype object". To get fileName and lineNumber
> properties in error objects without passing them to the constructor,
> you have to set the Context.FEATURE_LOCATION_INFORMATION_IN_ERROR
> property when you enter a Rhino context.
>

> http://www.mozilla.org/rhino/apidocs/org/mozilla/javascript/Context.h...

sk

unread,
May 26, 2010, 1:02:45 PM5/26/10
to Narwhal and Jack
Should I just be modifying engines/rhino/bootstraps.js?

I notice a narwhal.conf file as well which is the reason I ask.

Scott.

sk

unread,
May 26, 2010, 1:45:24 PM5/26/10
to Narwhal and Jack
I should mention that I'm using Windows as my development environment,
and this might be the problem.

local narwhal.conf files and the engines/rhino/bootstrap.js don't seem
to be used in windows machines, so I'm not sure how to change rhino's
behavior.

What's worse is that the virtual environments feature isn't really
working properly on windows either. ie, require calls don't get
picked up for local lib/script files. One has to place them into jack/
lib to see them.

sk

unread,
May 26, 2010, 3:10:54 PM5/26/10
to Narwhal and Jack
Hannes,

Can you provide example code of how you set the
Context.FEATURE_LOCATION_INFORMATION_IN_ERROR in code?

I see in Ringo you've overridden Context factory, but I'm hoping that
in narwhal's bootstrap.js file somehow I can just tell the current
context to turn this feature on? Since it's a static final though I'm
assuming it's getting it from config somewhere, but I can't see where
narwhal is providing this info.

Thanks.
Scott.

On May 26, 1:01 am, Hannes Wallnoefer <han...@helma.at> wrote:
> 2010/5/26 sk <doyouunderst...@gmail.com>:
>
>
>
>
>

> > Given:
>
> > exports.app =  function(env) {
> >        throw new Error("an error occurred");
> >        return {
> >          status : 200,
> >          headers : {"Content-Type":"text/plain"},
> >          body : ["test"]
> >        };
> > };
>
> > How can I get the line number and file name of the exception?  It's
> > especially critical for nested files and modules.
>
> > Currently it just prints:
>
> > Error: an error occurred
>
> > and the fileName and lineNumber properties of the exception object
> > (inside ShowExceptions middleware) are null.
>
> > RhinoExceptions would have a stack trace, but what about exceptions in
> > my own js?
>
> Rhino by default follows the ES spec maybe to closely, which says
> "Error instances have no special properties beyond those inherited
> from the Error prototype object". To get fileName and lineNumber
> properties in error objects without passing them to the constructor,
> you have to set the Context.FEATURE_LOCATION_INFORMATION_IN_ERROR
> property when you enter a Rhino context.
>

> http://www.mozilla.org/rhino/apidocs/org/mozilla/javascript/Context.h...

Kris Kowal

unread,
May 26, 2010, 3:39:06 PM5/26/10
to narw...@googlegroups.com
On Wed, May 26, 2010 at 10:02 AM, sk <doyouun...@gmail.com> wrote:
> Should I just be modifying engines/rhino/bootstraps.js?
> I notice a narwhal.conf file as well which is the reason I ask.

If you can get all errors to have backtraces by modifying
bootstrap.js, we'd be happy to merge it, for sure. I haven't had any
problem with that on other platforms, though, so I'm curious why it's
not working on Windows. Apart from that, it would be awesome if
someone could get the Windows bootstrapping to be an exact port of the
Unix bootstrapping. It's been 15 years since I wrote a batch file in
earnest.

Kris Kowal

Scott Klarenbach

unread,
May 26, 2010, 3:53:56 PM5/26/10
to narw...@googlegroups.com
Kris,

So in your environment when you throw new Error() you see a full stacktrace in the browser?  I'm using the jack/showexceptions middleware but see nothing except a 1-liner with the exception message.

At this point I'm looking into overridding ContextFactory but if it's already working in *nix then I'm barking up the wrong tree.

As mentioned earlier, Windows has a lot of virtual environment bugs and things that are missing, so maybe it's just something in those .cmd files that needs to be fixed to get the behavior to work the same as other operating systems.

Scott.

--
You received this message because you are subscribed to the Google Groups "Narwhal and Jack" group.
To post to this group, send email to narw...@googlegroups.com.
To unsubscribe from this group, send email to narwhaljs+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/narwhaljs?hl=en.




--
Talk to you soon,

Scott Klarenbach

PointyHat Software Corp.
www.pointyhat.ca
p 604-568-4280
e sc...@pointyhat.ca
#200 - 3466 W. Broadway
Vancouver, BC V6R2B3

_______________________________________
To iterate is human; to recurse, divine

Hannes Wallnoefer

unread,
May 26, 2010, 4:08:46 PM5/26/10
to narw...@googlegroups.com
2010/5/26 sk <doyouun...@gmail.com>:

> Hannes,
>
> Can you provide example code of how you set the
> Context.FEATURE_LOCATION_INFORMATION_IN_ERROR in code?
>
> I see in Ringo you've overridden Context factory, but I'm hoping that
> in narwhal's bootstrap.js file somehow I can just tell the current
> context to turn this feature on?  Since it's a static final though I'm
> assuming it's getting it from config somewhere, but I can't see where
> narwhal is providing this info.

Looking at the code, I'm afraid the only way to activate this feature
is to subclass either Rhino's Context or ContextFactory class,
overriding the hasFeature() method. You can do a lot from JS in Rhino,
but the Context/ContextFactory code runs before you enter any JS, so
you have to write some Java eventually.

Hannes

Scott Klarenbach

unread,
May 26, 2010, 4:09:29 PM5/26/10
to narw...@googlegroups.com
Ya, that's the same sad conclusion I came to, just wanted confirmation before I did that.

Thanks!

Scott.

Scott Klarenbach

unread,
May 26, 2010, 4:18:10 PM5/26/10
to narw...@googlegroups.com
Surely though I expected they'd have a simple config file somewhere.

Subclassing a ContextFactory to change the default value of an int flag is ridiculous, even for mozilla ;).

sk

sk

unread,
May 26, 2010, 8:05:01 PM5/26/10
to Narwhal and Jack
Well,

I tried overridding both Context and ContextFactory in bootstrap.js in
order to provide my own implementation of hasFeature, but neither
worked. No stack traces in either instance, even though
Context.FEATURE_LOCATION_INFORMATION_IN_ERROR was set to true.

I just can't believe that mozilla doesn't have a config property for
this.

I'm at a loss at this point, and without stack traces don't think I
can build anything using Jack on windows. Maybe after some sleep
something will come to me.

Scott.

Dean Landolt

unread,
May 26, 2010, 9:31:58 PM5/26/10
to narw...@googlegroups.com
Scott

I get stack traces in narwhal on windows -- and IIRC I used to in jack. I do not however get them in pintura for some reason -- perhaps it's related to a change. I'll poke at this soon but it's certainly not insurmountable.

sk

unread,
May 26, 2010, 9:41:51 PM5/26/10
to Narwhal and Jack
Thanks Dean.

I have stack traces too now. :) There was a bug w/ my java factory
implementation, which meant that my overridden hasFeature wasn't being
called at the right time.

So, I have stack traces for all the built in javascript errors
(syntax, type, etc), but for custom exceptions (such as the example I
started this thread with, ie: throw new Error("crash!"), there is no
trace. This is much less critical as developers can usually be
expected to provide some context manually, but I wonder if there's yet
another bug I'm missing, or if currently any exceptions thrown by your
own code are not going to be traced through Rhino?

Thanks again for all the help you guys.

sk

On May 26, 6:31 pm, Dean Landolt <d...@deanlandolt.com> wrote:

> > narwhaljs+...@googlegroups.com<narwhaljs%2Bunsubscribe@googlegroups .com>


> > .
> > > >> > For more options, visit this group athttp://
> > groups.google.com/group/narwhaljs?hl=en.
>
> > > > --
> > > > You received this message because you are subscribed to the Google
> > Groups "Narwhal and Jack" group.
> > > > To post to this group, send email to narw...@googlegroups.com.
> > > > To unsubscribe from this group, send email to

> > narwhaljs+...@googlegroups.com<narwhaljs%2Bunsubscribe@googlegroups .com>


> > .
> > > > For more options, visit this group athttp://
> > groups.google.com/group/narwhaljs?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Narwhal and Jack" group.
> > To post to this group, send email to narw...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > narwhaljs+...@googlegroups.com<narwhaljs%2Bunsubscribe@googlegroups .com>

Hannes Wallnoefer

unread,
May 27, 2010, 1:36:38 AM5/27/10
to narw...@googlegroups.com
2010/5/27 sk <doyouun...@gmail.com>:

> Thanks Dean.
>
> I have stack traces too now. :)  There was a bug w/ my java factory
> implementation, which meant that my overridden hasFeature wasn't being
> called at the right time.
>
> So, I have stack traces for all the built in javascript errors
> (syntax, type, etc), but for custom exceptions (such as the example I
> started this thread with, ie: throw new Error("crash!"), there is no
> trace.  This is much less critical as developers can usually be
> expected to provide some context manually, but I wonder if there's yet
> another bug I'm missing, or if currently any exceptions thrown by your
> own code are not going to be traced through Rhino?

Wait - for syntax and type errors and exceptions thrown in Java you
should have had location information and stack traces all along. The
feature we talked about _only_ affects errors created via new Error()
constructor.

So if you don't get file names and line numbers for new Error(), your
ContextFactory is still not doing anything, which is what I'd expect
if you set it from JavaScript (i.e. when a context has already been
created and entered). That's just a chicken/egg problem with the way
narwhal's rhino bootstrap mechanism works.

Hannes

> To unsubscribe from this group, send email to narwhaljs+...@googlegroups.com.

Scott Klarenbach

unread,
May 27, 2010, 2:29:08 AM5/27/10
to narw...@googlegroups.com
Very strange Hannes.

On my machine, I don't see a stack trace for ANY exceptions, built-in or otherwise.

To be clear,  I extended the mozilla Context class and implemented a custom hasFeature method.  I then modified the bootstrap.js file to use this newly created StackTraceContext class.  Once I do this, only then, do I begin to see stack traces but only for the BUILT-IN javascript errors.
If I revert the bootstrap.js file to use the original mozilla Context, stack traces disappear again entirely.

But, even after extending Context with my own class, the custom throw new Error() messages show no stack.

This is on Windows 7, so maybe it's unique to that.  But Dean said he had stack traces working on Windows.

Dean, did you mean for built in Errors or custom ones?

Scott.
Reply all
Reply to author
Forward
0 new messages