Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

syntax errors are printed to stderr, even when wrapped with try/catch #6920

Closed
sam-github opened this issue Jan 20, 2014 · 5 comments
Closed
Assignees
Labels
Milestone

Comments

@sam-github
Copy link

I would expect no output to stderr when syntax or other exceptions are caught.

% node -e "try{require('/etc/passwd')}catch(e){}"

/etc/passwd:1
unction (exports, require, module, __filename, __dirname) { root:x:0:0:root:/r

Also, not that the missing f before "function" is not a cut-n-paste error, I'm not sure why that is happening, possibly because of binary in my passwd file.

@dougwilson
Copy link
Member

This is how JavaScript works. You cannot catch a SyntaxError, as this is throw at the time the source is parsed. The try/catch only applies to runtime errors.

@sam-github
Copy link
Author

a) the call to the require function happens at runtime, and b) the syntax error is caught, as you can see below:

% node -e 'try{require("/etc/passwd")}catch(e){console.log("<%s>",e)} console.log("caught it")'

/etc/passwd:1
unction (exports, require, module, __filename, __dirname) { root:x:0:0:root:/r
                                                                    ^
<SyntaxError: Unexpected token :>
caught it
%

You are probably thinking of examples like this:

try {
  some random text that is not js syntax
} catch(e) {
/// this will not be reached, nothing will, the code won't compile
}

But that's not what is happening.

@rlidwka
Copy link

rlidwka commented Jan 21, 2014

Also, not that the missing f before "function" is not a cut-n-paste error, I'm not sure why that is happening

Just for the record: it's not a bug, pretty-printer just ate first "f" to fit the error in ~80 characters.

@domenic
Copy link

domenic commented Feb 2, 2014

This is caused by #1310. Also relevant: #5848.

indutny added a commit to indutny/node that referenced this issue Feb 5, 2014
Try embedding the ` ... ^` lines inside the `SyntaxError` (or any other
native error) object before giving up and printing them to the stderr.

fix nodejs#6920
fix nodejs#1310
@indutny
Copy link
Member

indutny commented Feb 5, 2014

Should be fixed by #7049

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants