capturing errors on load() from Shell

35 views
Skip to first unread message

Chris Winters

unread,
Feb 9, 2012, 5:36:50 PM2/9/12
to mozilla-rhino
Currently errors encountered when trying to load() a separate JS file
get written to STDERR. but there's no return value or exception to
indicate an error. Is there any way to get an indication that load()
failed? (I tried running with '-fatal-warnings' and '-strict', no
luck.)

Before running load() I even tried temporarily swapping out System.err
for my own PrintStream (evil, I know) but that captured nothing.

Thanks much for any help.

Chris

Chris Winters

unread,
Feb 13, 2012, 9:15:48 AM2/13/12
to mozilla-rhino
FWIW, I plan on getting around this by reading in the content myself
(just using the local filesystem, no remote loading) and then using
eval() to bring the code in. That will throw exceptions as expected,
though not as informative as the STDERR output from load(), but at
least I can capture it.

Chris

Hannes Wallnöfer

unread,
Feb 13, 2012, 9:18:39 AM2/13/12
to mozill...@googlegroups.com
Just to let you know: I'm fixing this right now and will soon push a
fix to github.

Hannes

2012/2/13 Chris Winters <chris....@gmail.com>:

Chris Winters

unread,
Feb 13, 2012, 9:50:10 AM2/13/12
to mozilla-rhino
Thank you! I've used rhino for a number of years and this has always
been a little annoyance. But of course, if nobody brings it up nobody
knows it's an annoyance and it won't get fixed.

Chris

On Feb 13, 9:18 am, Hannes Wallnöfer <hann...@gmail.com> wrote:
> Just to let you know: I'm fixing this right now and will soon push a
> fix to github.
>
> Hannes
>
> 2012/2/13 Chris Winters <chris.wint...@gmail.com>:

Chris Winters

unread,
Feb 13, 2012, 10:25:00 AM2/13/12
to mozilla-rhino
Interestingly, this works for some libraries but not others. It seems
to be related to how the library interacts with the global namespace.
For example, if I have a library that defines a global var as a pseudo-
namespace it works fine:

var Foo = {}
Foo.someFunction = function() ...;

But other libraries do not. For example, underscore.js does (I think)
some work to check to see if it's running in a CommonJS module
container and defines the '_' differently as a result. Using the slurp
+ eval() trick does not register that in the global namespace, but
using load() does.

Unsure if it's related, but when running in strict mode (with the '-
strict' command-line flag) Rhino issues a number of 'Code has no side
effects' errors when trying to bring in underscore via any means.

Chris

[1] http://documentcloud.github.com/underscore/

Hannes Wallnoefer

unread,
Feb 16, 2012, 5:50:39 AM2/16/12
to mozilla-rhino
Sorry, it's taking a bit longer than planned - turned out some other
code relies on shared internal methods to be non-throwing, but I hope
I can fix this later today.

Hannes

Chris Winters

unread,
Feb 27, 2012, 3:51:29 PM2/27/12
to mozilla-rhino
I hadn't previously seen the discussion of implementing 'require'.
It's a separate feature (e.g., run with '-require' and '-modules' to
specify paths) and requires the loaded file to know it's a CommonJS
module, but it's worth noting that it does throw an exception if the
module has compilation errors. Great!

It also prints out more detailed information to STDERR, and it seems
like that same information should be available in the exception but I
can't figure out how to extract it. Any tips?

Thanks,

Chris

Hannes Wallnöfer

unread,
May 3, 2012, 11:51:06 AM5/3/12
to mozill...@googlegroups.com
It's taken a long time, but I finally pushed the commit that makes
load() throw errors instead of catching them. The reason it took so
long was that load() shares its implementation with the code that
evaluates command line scripts, shell input, and inline scripts. In
fact this changes the behavior or some public methods in
tools.shell.Main, but I think it makes more sense this way.

https://github.com/mozilla/rhino/commit/a9db4879b9a4bb8177600840f1e27c8c5360a731

Regarding reporting of additional compilation errors: I don't thinks
these are available in the exception/error object. They are reported
through Rhino's ErrorReporter interface. You can set the ErrorReporter
through the setErrorReporter method in your Rhino Context.

Hannes

2012/2/27 Chris Winters <chris....@gmail.com>:
Reply all
Reply to author
Forward
0 new messages