Javascript eval()异常 - 行号 [英] Javascript eval() Exception - line number

查看:166
本文介绍了Javascript eval()异常 - 行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中,我有一个 var str =.a长字符串,包含许多行...
如果由 eval(str);

In JavaScript I have a var str = ".a long string that contains many lines..." In case of exception that caused by eval(str);

我想抓住它并打印引起异常的行号。 (str ..内部的行)

I had like to catch it and print the the line number that caused the exception. (the line internal to str..)

可以吗?

编辑作为Alligator项目的一部分( http://github.com/mrohad/Alligator ),应用程序服务器的JavaScript,我正在从磁盘读取文件和eval()任何嵌套到scriplet(<??>)

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 a scriplet( < ? ? > )

我正在外面运行这个脚本一个浏览器,使用NodeJS(在V8之上)。

I am running this script outside a browser, using NodeJS (on top of V8).

推荐答案

我发现一个非常低效的解决方案,但我只使用它当debug_mode == 1所以它不是那么糟糕。

I found a solution which is pretty inefficient, yet I only use it when debug_mode==1 so it's not that bad..

我将eval_str写入一个文件,我导入该文件,并在try {}中调用它catch {}并从堆栈跟踪中解析错误行...

I write the eval_str to a file, I "import that file, and invoke it inside a try{}catch{} and I parse the error line from the stack trace...

在我的具体情况下,代码如下:

In my specific case, this is how the code looks like:

var errFileContent = "exports.run = "+evalStringAsAFunction+";";
fs.writeFile('/home/vadmin/Alligator/lib/debugging.js', errFileContent, function (err) {
    var debug = require('./debugging');
    try{
         debug.run(args...);
    }
    catch(er){
         log.debug(parseg(er));
    }
});

这篇关于Javascript eval()异常 - 行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆