Javascript eval() 异常 - 行号

发布于 2024-09-14 14:27:50 字数 463 浏览 4 评论 0原文

在 JavaScript 中,我有一个 var str = ".a long string that contains much rows..." 如果发生由 eval(str); 引起的异常,

我想捕获它并打印导致异常的行号。 (str 内部的行..)

有可能吗?

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

我在浏览器外部运行此脚本,使用 NodeJS (在V8 顶部)。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

誰ツ都不明白 2024-09-21 14:27:50

尝试将 try/catch 添加到字符串而不是 eval 周围:

var code = 'try{\nvar c = thisFuncIsNotDefined();\n}catch(e){alert(e.lineNumber);}';

Try adding the try/catch to the string instead of around the eval:

var code = 'try{\nvar c = thisFuncIsNotDefined();\n}catch(e){alert(e.lineNumber);}';
雨轻弹 2024-09-21 14:27:50

1)运行:

var javascript_offset;
try {
  undefined_function();
} catch(ex1) {
  javascript_offset = ex1.lineNumber;
}
try {
  YOUR_STRING_WITH_JS
} catch (ex2) {
  var line_that_caused_it = ex2.lineNumber - javascript_offset -2;
  HANDLE_THE_EXCEPTION_HERE
}

1) Run:

var javascript_offset;
try {
  undefined_function();
} catch(ex1) {
  javascript_offset = ex1.lineNumber;
}
try {
  YOUR_STRING_WITH_JS
} catch (ex2) {
  var line_that_caused_it = ex2.lineNumber - javascript_offset -2;
  HANDLE_THE_EXCEPTION_HERE
}
拥抱我好吗 2024-09-21 14:27:50

我找到了一个效率很低的解决方案,但我只在 debug_mode==1 时使用它,所以它还不错。

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

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

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));
    }
});

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

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));
    }
});
晌融 2024-09-21 14:27:50
replaceErrors(key, value) {
    if (value instanceof Error) {
      var error = {};
      Object.
        getOwnPropertyNames(value).
        forEach(function (key) {
          error[key] = value[key];
        });
      return error;
    }
    return value;
  }
const errorStr = JSON.stringify(error.stack, this.replaceErrors);
const regexp = 'your reg';
          let isHasLineNum = regexp.exec(errorStr);
          let lineNum
          if (isHasLineNum) {
            lineNum = isHasLineNum[0];
          }
replaceErrors(key, value) {
    if (value instanceof Error) {
      var error = {};
      Object.
        getOwnPropertyNames(value).
        forEach(function (key) {
          error[key] = value[key];
        });
      return error;
    }
    return value;
  }
const errorStr = JSON.stringify(error.stack, this.replaceErrors);
const regexp = 'your reg';
          let isHasLineNum = regexp.exec(errorStr);
          let lineNum
          if (isHasLineNum) {
            lineNum = isHasLineNum[0];
          }
大姐,你呐 2024-09-21 14:27:50

这能解决你的问题吗?

try {
    eval(str);
} catch (e) {
    console.log(e.lineNumber)
}

This solves your problem?

try {
    eval(str);
} catch (e) {
    console.log(e.lineNumber)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文