Javascript Eval Syntax Error Line Number
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack eval alternative javascript Overflow the company Business Learn more about hiring developers or posting ads with us
Javascript Error Line Number
Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a
Error Object Javascript
community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Javascript eval() Exception - line number up vote 24 down vote favorite 5 In JavaScript I
Js Eval
have a var str = ".a long string that contains many lines..." In case of exception that caused by eval(str); I had like to catch it and print the the line number that caused the exception. (the line internal to str..) Is it possible? EDIT As part of the Alligator project (http://github.com/mrohad/Alligator), an application server for JavaScript, I am reading files from the disk and eval() anything that is nested to javascript try a scriplet( < ? ? > ) I am running this script outside a browser, using NodeJS (on top of V8). javascript exception eval node.js v8 share|improve this question edited Aug 15 '10 at 20:46 asked Aug 15 '10 at 19:59 DuduAlul 3,08642654 2 Not sure this can be done at all without splitting the multi-line command into separate instructions and eval() calls. –Pekka 웃 Aug 15 '10 at 20:01 2 It's pretty ugly to use "eval" in the first place. Perhaps you could describe what you're trying to do, and somebody might have a better idea. –Pointy Aug 15 '10 at 20:06 The problem with splitting the eval to multi lines is for example when you have if(cond) <- line #1 {some code} <- line #2, line 1 will throw exception since it depends on line #2 –DuduAlul Aug 15 '10 at 20:12 1 I assumed that in an age of AJAX someone would want a solution to this. I am having the same problem –puk Oct 22 '11 at 23:30 1 @MrOhad - checkout this Google V8 issue code.google.com/p/v8/issues/detail?id=1914 –BMiner May 30 '12 at 14:11 add a comment| 4 Answers 4 active oldest votes up vote 4 down vote Try adding the
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn javascript throw exception more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Line number of SyntaxError in Node.js up vote 12 down http://stackoverflow.com/questions/3488994/javascript-eval-exception-line-number vote favorite I have some code that uses vm module and runInNewContext function and executes dynamically generated JavaScript code. Basically a safer option of eval. The code (variable code) can possibly contain syntax errors, so I would like to catch them and print some useful information. try { vm.runInNewContext(code, sandbox, filename); } catch (e) { if (e instanceof SyntaxError) { // always false console.log(e.toString()); // "SyntaxError: Unexpected token http://stackoverflow.com/questions/20570477/line-number-of-syntaxerror-in-node-js ||" for example console.log(e.line); // how to get the line number? } } I'd like to print the number of the line with the syntax error, but I have two problems: I don't know how to recognize whether the exception is SyntaxError or something else. instaceof doesn't work (update - I can use e.name === "SyntaxError"). Even if I was able to recognize it, how could I get the line number? Is it possible? Thanks in advance. Update: I can get some information from e.stack - however, the topmost call in the stack trace is runInNewContext (with its line number), but I still can't find the line number inside code, which caused the exception (SyntaxError). javascript node.js exception v8 share|improve this question edited Dec 22 '13 at 20:10 asked Dec 13 '13 at 15:47 Martin Majer 1,12121127 Unfortunately, the information you are looking for cannot currently be obtained from the SyntaxError object: code.google.com/p/v8/issues/detail?id=2589 –Will Aug 14 '14 at 21:24 add a comment| 4 Answers 4 active oldest votes up vote 2 down vote 1) Use if (e.name === "SyntaxError"). 2) All data associated with the error is kept in e.stack. share|improve this answer answered Dec 13 '13 at 15:59 freakish 28.6k2
Sign in Pricing Blog Support Search GitHub This repository https://github.com/nodejs/node-v0.x-archive/issues/229 Watch 2,395 Star 36,942 Fork 8,327 nodejs/node-v0.x-archive Code Issues 570 Pull requests 41 Projects 0 Wiki Pulse Graphs New issue get line number http://unix.stackexchange.com/questions/104564/understanding-line-numbers-reported-when-using-eval from failed Script eval ? #229 Closed weepy opened this Issue Aug 4, 2010 · 4 comments Projects None yet Labels None error line yet Milestone No milestone Assignees No one assigned 3 participants weepy commented Aug 4, 2010 HI I'm doing somethign like this : try { process.binding('evals').Script.runInThisContext(text) } catch(e) { sys.p(e) } I'd like to see where in the text the error occured (in the error line number browser you tend to get a line number property) ? Is this possible ? I also tried new Function(text) and got the same output. Thanks weepy ghost commented Feb 17, 2011 forks for me on node 0.4.0 for this code: var text = 'console.log("start");\n\nconsole.looog();'; try { process.binding('evals').Script.runInThisContext(text) } catch(e) { console.log(JSON.stringify(e)) } I get the expected output, containing the information you need: start {"stack":"TypeError: Object # has no method 'looog'\n at evalmachine.:3:9\n at Object. (/home/tahu/nodetesting/b.js:4:37)\n at Module._compile (module.js:373:26)\n at Object..js (module.js:379:10)\n at Module.load (module.js:305:31)\n at Function._load (module.js:271:10)\n at Array.0 (module.js:392:10)\n at EventEmitter._tickCallback (node.js:108:26)","arguments":["looog",{}],"type":"undefined_method","message":"Object # has no method 'looog'"} isaacs closed this Mar 29, 2011 bminer commented May 6, 2012 Related issue: #2734 Upstream issue in Google V8: http://code.google.com/p/v8/issues/detail?id=1914 bminer referenced this issue May 6, 2012 Closed vm.runInNewContext/createScript doesn't throw line numbers on syntax errors
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Understanding line numbers reported when using eval up vote 3 down vote favorite Okay, so I'm stuck working with a shell script that needs to use eval (I know, I know, ultimate evil etc…) however I'm having difficulty debugging some of the line numbers being thrown out by errors. For example: script.sh: eval: line 315: syntax error near unexpected token `(' The line definitely doesn't correspond to line 315 in script.sh as that's entirely unrelated to the error. So I threw in random new-lines to figure out which line is originating the error, and as you'd expect it's a line with an eval statement. However, when I output the code being passed into the eval, it doesn't even contain 200 lines, let alone 315+! So what's going on here? Is there a way to usefully use the line numbers being reported, are they somehow based on the position at which the eval statement is run or something? I don't seem to be able to see a pattern thus far. If I could I'd replace all the eval statements, but unfortunately the code being passed is dynamically generated (not from user-input, don't worry!), and needs to be able to manipulate variables in the same scope as the rest of the script, which seems to preclude using echo "$commands" | sh or similar alternatives. I also have the issue of compatibility, and unfortunately as evil as it is eval is widely available. shell-script debugging share|improve this question asked Dec 10 '13 at 15:51 Haravikk 15211 1 Could you post a minimal working example that reproduces the issue? –terdon♦ Dec 10 '13 at 16:02 yes, line numbers when using eval are random with bash, try to interpret the script with a compatible shell, zsh in bash emulation mode and ksh93 come to mind. –Stéphane Chazelas Dec 10 '13 at 16:06 What if you execute your script with printing line numbers for all executed lines instead of just getting the line number where the error happened. Like here: stackoverflow.com/a/17805088/184968. It might give you an idea