Closed Bug 105438 Opened 23 years ago Closed 22 years ago

SourceName and lineNumbers of syntax errors in Javascript files not dispalyed.

Categories

(Rhino Graveyard :: Core, defect)

x86
Windows 2000
defect
Not set
major

Tracking

(Not tracked)

VERIFIED FIXED

People

(Reporter: sonali, Assigned: norrisboyd)

Details

Attachments

(1 file)

I'm using IBM's Bean Scripting Framework 2.2 for loading Rhino's js.jar 1.5, for 
evaluating Javascript files. However if a Syntax error occurs in the Javascript, 
the sourceName and line number of the error is not displayed. 
The following is the StackTrace I get:

SyntaxError: missing ; before statement
        at org.mozilla.javascript.NativeGlobal.constructError(NativeGlobal.java:
498)
        at org.mozilla.javascript.TokenStream.reportSyntaxError(TokenStream.java
:1356)
        at org.mozilla.javascript.Parser.reportError(Parser.java:76)
        at org.mozilla.javascript.Parser.wellTerminated(Parser.java:307)
        at org.mozilla.javascript.Parser.statementHelper(Parser.java:790)
        at org.mozilla.javascript.Parser.statement(Parser.java:337)
        at org.mozilla.javascript.Parser.parseFunctionBody(Parser.java:180)
        at org.mozilla.javascript.Parser.function(Parser.java:245)
        at org.mozilla.javascript.Parser.parse(Parser.java:122)
        at org.mozilla.javascript.Context.compile(Context.java:1834)
        at org.mozilla.javascript.Context.compile(Context.java:1782)
        at org.mozilla.javascript.Context.compileReader(Context.java:822)
        at org.mozilla.javascript.Context.evaluateReader(Context.java:738)
        at org.mozilla.javascript.Context.evaluateString(Context.java:705)
        at com.ibm.bsf.engines.javascript.JavaScriptEngine.eval(JavaScriptEngine
.java:83)
        at com.ibm.bsf.util.BSFEngineImpl.exec(BSFEngineImpl.java:106)

I found that this can be partially fixed in file EcmaError.java, method 
toString() line 86, by returning the line number and the columnNumber along with 
the errorMessage, even when the sourceName is null.

REPLACE:
return errorObject.toString();
WITH
return errorObject.toString() + "( line " + lineNumber + " column " + 
columnNumber + ")" ; 

Please fix this problem as it is very essential to know the line number of the 
error. 
Also, the sourceName is null in this case. Why ?
Note bug 50447 against SpiderMonkey may be of interest. The fix there
was to add new properties to Error objects: 'fileName' and 'lineNumber'.
(I don't know if added to Error.prototype or to Error objects themselves.)

At any rate, there is a testcase for it now:

       mozilla/js/tests/js1_5/Exceptions/regress-50447.js

I added this to rhino-n.tests skip list, since these new properties
are non-ECMA extensions. If Rhino decides to implement them, we could
use this testcase in Rhino, too -
I just checked in a fix.
Status: NEW → RESOLVED
Closed: 23 years ago
Resolution: --- → FIXED
Trying to verify this. If I launch the Rhino shell and load a file
interactively, I do see a "file name" and line number:

[] java org.mozilla.javascript.tools.shell.Main
js> load('../../tests/test/test/bug.js')
js: "<stdin>", line 1: uncaught JavaScript exception: SyntaxError:
                       Invalid assignment left-hand side. (<stdin>; line 1)


But if I load the file via the option -f, I don't get the same info:

[] java org.mozilla.javascript.tools.shell.Main -f ../../tests/test/test/bug.js
js: uncaught JavaScript exception: SyntaxError: 
                                   Invalid assignment left-hand side.


Should we reopen this bug for consideration of this case?
Or is it just a problem with the shell and not Rhino proper?  
I guess I'll take the plunge and reopen. This is SpiderMonkey in the same
two situations:

INTERACTIVE LOADING
[] ./js
js> load('../../tests/test/test/bug.js')
../../tests/test/test/bug.js:34: SyntaxError: invalid assignment left-hand side:
../../tests/test/test/bug.js:34: 1 = 2;
../../tests/test/test/bug.js:34: ..^

LOADING VIA -f OPTION
[] ./js -f ../../tests/test/test/bug.js
../../tests/test/test/bug.js:34: invalid assignment left-hand side:
../../tests/test/test/bug.js:34: 1 = 2;
../../tests/test/test/bug.js:34: ..^
../../tests/test/test/bug.js:34: SyntaxError: invalid assignment left-hand side:
../../tests/test/test/bug.js:34: 1 = 2;
../../tests/test/test/bug.js:34: ..^
Status: RESOLVED → REOPENED
Resolution: FIXED → ---
I keep getting syntax errors with no line numbers as well.

That happens when I use Context.compileReader(..) to compile the script. The
DefaultErrorReporter will throw an exception with only the message and not the
line it happened on.

It is of course easy to workaround using your own error reporter, but I've
attached a patch to add on the line and source name so the DefaultErrorReporter
gives the similar output as EcmaError if that is wanted.
Thanks for the patch--I regresion tested and committed it. 
Status: REOPENED → RESOLVED
Closed: 23 years ago22 years ago
Resolution: --- → FIXED
Marking Verified Fixed.

Here is the effect of the fix, as I load bad syntax in Rhino;
first interactively via load(), then via the file option -f.
The file I'm loading has a deliberate error |1=2| on line 1:


-----------------------   BEFORE THE FIX  -------------------------------

[ ] java org.mozilla.javascript.tools.shell.Main
Rhino 1.5 release 4 0000 00 00 (in progress)
js> load('../../tests/BAD.js')
js: "<stdin>", line 6: uncaught JavaScript exception:
SyntaxError: Invalid assignment left-hand side. (<stdin>; line 6)


[ ] java org.mozilla.javascript.tools.shell.Main  -f ../../tests/BAD.js
js: uncaught JavaScript exception:
SyntaxError: Invalid assignment left-hand side.



-----------------------   AFTER THE FIX  -------------------------------

[ ] java org.mozilla.javascript.tools.shell.Main
Rhino 1.5 release 4 0000 00 00 (in progress)
js> load('../../tests/BAD.js')

js: "D:\JS_trunk\mozilla\js\tests\BAD.js",line 1:
uncaught JavaScript exception: SyntaxError: Invalid assignment left-hand side. 
(D:\JS_trunk\mozilla\js\tests\BAD.js; line 1)
js: 1=2;
js: ...^


[ ] java org.mozilla.javascript.tools.shell.Main  -f ../../tests/BAD.js
js: "D:\JS_trunk\mozilla\js\tests\BAD.js", line 1:
uncaught JavaScript exception: SyntaxError: Invalid assignment left-hand side. 
(D:\JS_trunk\mozilla\js\tests\BAD.js; line 1)
js: 1=2;
js: ...^
Status: RESOLVED → VERIFIED
NOTE: in my data above, I formatted the last two lines by hand
to look like this:

js: 1=2;
js: ...^


In actuality, they were reported like this:

js: 1=2;


js: ...^


QUESTION: do we want these two line feeds between the error and the caret? 
Targeting as resolved against 1.5R4
Target Milestone: --- → 1.5R4
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: