改进动态构建模块的错误报告。

发布于 2025-02-01 23:04:51 字数 2725 浏览 4 评论 0原文

我有一个应用程序,可以将单个较小的文件动态加载到模块中。运行时,它的运行良好已有几年了。

这是迭代文件夹,读取它们的代码的主要部分,然后将它们附加到所需的模块上。

  var path = module.path+"/lib"; 
  require('async').eachSeries(require('fs').readdirSync(path),function(file,next){
    require('fs').readFile(path+'/'+file,'utf8',function(err,code){
      var id = file.split('.')[0];
      if(id in module.exports) module.exports._errs.push({'lib':id,'err':'mod/'+module.name+'/lib/'+file.replace(/\.obj|\.fnc|\.str/,'.xxx')+' filename duplicated.'});
      try{module.exports[id] = eval('false ||'+code);}
      catch(err){
        module.exports[id]={'_err':err.message};
        module.exports._errs.push({'lib':id,'err':err.message})
      }
      next(); 
    })
  },function(){

这一切都起作用,但问题是当生成错误时,似乎所有文件 /对象 /函数都被匿名加载,这使得无法从堆栈跟踪错误确切确定哪个函数失败了,因为所有功能似乎都是匿名的。

是否可以修改代码,以便它们是非匿名的,也可以修改堆栈跟踪错误报告以显示实际导致崩溃的代码(最好是变量)?

TypeError: Cannot read property 'replace' of undefined
    at eval (eval at <anonymous> (eval at <anonymous> (/snapshot/src/x3-bin/source/mod/va/index.js:1:462)), <anonymous>:1:1642)
    at Query.<anonymous> (/snapshot/src/x3-bin/source/lib/mysql.js:1:2474)
    at Query.<anonymous> (/snapshot/src/x3-bin/source/node_modules/mysql/lib/Connection.js:502:10)
    at Query.domain [as _callback] (/snapshot/src/x3-bin/source/node_modules/mysql/lib/Connection.js:468:16)
    at Query.Sequence.end (/snapshot/src/x-bin/source/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
    at Query._handleFinalResultPacket (/snapshot/src/x3-bin/source/node_modules/mysql/lib/protocol/sequences/Query.js:139:8)
    at Query.EofPacket (/snapshot/src/x3-bin/source/node_modules/mysql/lib/protocol/sequences/Query.js:123:8)
    at Protocol._parsePacket (/snapshot/src/x-bin/source/node_modules/mysql/lib/protocol/Protocol.js:278:23)
    at Parser.write (/snapshot/src/x3-bin/source/node_modules/mysql/lib/protocol/Parser.js:76:12)
    at Protocol.write (/snapshot/src/x-bin/source/node_modules/mysql/lib/protocol/Protocol.js:38:16)
    at Socket.<anonymous> (/snapshot/src/x3-bin/source/node_modules/mysql/lib/Connection.js:91:28)
    at Socket.<anonymous> (/snapshot/src/x-bin/source/node_modules/mysql/lib/Connection.js:502:10)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)

如您所见,确定问题来源的信息非常模糊(评估),无法确切确定哪个模块。Exports功能是原因?

TypeError: Cannot read property 'replace' of undefined
    at eval (eval at <anonymous> (eval at <anonymous> (/snapshot/src/x3-bin/source/mod/va/index.js:1:462)), <anonymous>:1:1642)

I have an app that dynamically loads individual smaller files into module.export at runtime, it has been working well for a couple of years.

This is the main part of the code that iterates folders, reads them, and then appends them to the desired module.exports after a bit of validation.

  var path = module.path+"/lib"; 
  require('async').eachSeries(require('fs').readdirSync(path),function(file,next){
    require('fs').readFile(path+'/'+file,'utf8',function(err,code){
      var id = file.split('.')[0];
      if(id in module.exports) module.exports._errs.push({'lib':id,'err':'mod/'+module.name+'/lib/'+file.replace(/\.obj|\.fnc|\.str/,'.xxx')+' filename duplicated.'});
      try{module.exports[id] = eval('false ||'+code);}
      catch(err){
        module.exports[id]={'_err':err.message};
        module.exports._errs.push({'lib':id,'err':err.message})
      }
      next(); 
    })
  },function(){

This all works, but the issue is when there is an error generated, it appears that all of the files / objects / functions are loaded anonymously which make it impossible to determine from the stack trace errors exactly which function failed, as all appear to be anonymous.

Is it possible to either modify the code so that they are non-anonymous or alternatively modify the stack trace error reporting to show the code (& preferably variables) that actually caused the crash ??

TypeError: Cannot read property 'replace' of undefined
    at eval (eval at <anonymous> (eval at <anonymous> (/snapshot/src/x3-bin/source/mod/va/index.js:1:462)), <anonymous>:1:1642)
    at Query.<anonymous> (/snapshot/src/x3-bin/source/lib/mysql.js:1:2474)
    at Query.<anonymous> (/snapshot/src/x3-bin/source/node_modules/mysql/lib/Connection.js:502:10)
    at Query.domain [as _callback] (/snapshot/src/x3-bin/source/node_modules/mysql/lib/Connection.js:468:16)
    at Query.Sequence.end (/snapshot/src/x-bin/source/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
    at Query._handleFinalResultPacket (/snapshot/src/x3-bin/source/node_modules/mysql/lib/protocol/sequences/Query.js:139:8)
    at Query.EofPacket (/snapshot/src/x3-bin/source/node_modules/mysql/lib/protocol/sequences/Query.js:123:8)
    at Protocol._parsePacket (/snapshot/src/x-bin/source/node_modules/mysql/lib/protocol/Protocol.js:278:23)
    at Parser.write (/snapshot/src/x3-bin/source/node_modules/mysql/lib/protocol/Parser.js:76:12)
    at Protocol.write (/snapshot/src/x-bin/source/node_modules/mysql/lib/protocol/Protocol.js:38:16)
    at Socket.<anonymous> (/snapshot/src/x3-bin/source/node_modules/mysql/lib/Connection.js:91:28)
    at Socket.<anonymous> (/snapshot/src/x-bin/source/node_modules/mysql/lib/Connection.js:502:10)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)

As you can see, the info to identify the source of the problem is very vague (eval at ) and impossible to identify exactly which module.exports function was the cause ?

TypeError: Cannot read property 'replace' of undefined
    at eval (eval at <anonymous> (eval at <anonymous> (/snapshot/src/x3-bin/source/mod/va/index.js:1:462)), <anonymous>:1:1642)

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

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

发布评论

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

评论(1

晚雾 2025-02-08 23:04:52

是否可以修改代码,以使它们不匿名

函数(err,code){...}是一个匿名函数,函数notanymon(err,code) {...}不是。请参阅此示例节点会话:

> (function() { eval("let x; x.replace;"); })()
Uncaught TypeError: Cannot read properties of undefined (reading 'replace')
    at eval (eval at <anonymous> (REPL9:1:15), <anonymous>:1:10)
    at REPL9:1:15
> (function NotAnonymous() { eval("let x; x.replace;"); })()
Uncaught TypeError: Cannot read properties of undefined (reading 'replace')
    at eval (eval at NotAnonymous (REPL10:1:28), <anonymous>:1:10)
    at NotAnonymous (REPL10:1:28)
> (function NotAnonymous() { eval("(function AlsoNicelyNamed() {let x; x.replace;})()"); })()
Uncaught TypeError: Cannot read properties of undefined (reading 'replace')
    at AlsoNicelyNamed (eval at NotAnonymous (REPL11:1:28), <anonymous>:1:39)
    at eval (eval at NotAnonymous (REPL11:1:28), <anonymous>:1:49)
    at NotAnonymous (REPL11:1:28)

请注意第二版和第三版本如何包含摘要evalion

&lt; anonymous&gt;&lt; anonymous&gt;:1:10在我的示例中在您的)中,是由于获得eval ed的字符串没有文件名。为了更改,您必须停止使用eval。不过,行/列信息应该是准确的(至少是当我将\ n放入测试字符串中时);因此,看起来您在此特定示例中加载的文件被缩小了,第一行中至少有1642个字符。

不可能确切识别哪个模块函数是原因?

您已经有了这些信息:在

module.exports._errs.push({'lib':id,'err':err.message})
ID是导致问题的导出。

除此之外,这实际上取决于您eval ing的文件中的内容。如果其中有匿名功能,那么它们当然会显示为“匿名”,并且您必须通过提供所有功能的名称来解决此处。

Is it possible to either modify the code so that they are non-anonymous

Well, function(err,code){...} is an anonymous function, function NotAnonymous(err, code){...} is not. See this example Node session:

> (function() { eval("let x; x.replace;"); })()
Uncaught TypeError: Cannot read properties of undefined (reading 'replace')
    at eval (eval at <anonymous> (REPL9:1:15), <anonymous>:1:10)
    at REPL9:1:15
> (function NotAnonymous() { eval("let x; x.replace;"); })()
Uncaught TypeError: Cannot read properties of undefined (reading 'replace')
    at eval (eval at NotAnonymous (REPL10:1:28), <anonymous>:1:10)
    at NotAnonymous (REPL10:1:28)
> (function NotAnonymous() { eval("(function AlsoNicelyNamed() {let x; x.replace;})()"); })()
Uncaught TypeError: Cannot read properties of undefined (reading 'replace')
    at AlsoNicelyNamed (eval at NotAnonymous (REPL11:1:28), <anonymous>:1:39)
    at eval (eval at NotAnonymous (REPL11:1:28), <anonymous>:1:49)
    at NotAnonymous (REPL11:1:28)

Note how the second and third versions contain the snippet eval at NotAnonymous.

The other occurrence of <anonymous> (<anonymous>:1:10 in my example, <anonymous>:1:1642 in yours) is due to the fact that strings that get evaled don't have a file name. To get change that, you'd have to stop using eval. The line/column information should be accurate though (at least, it is when I put an \n into a test string); so it looks like the file you loaded in this particular example was minified and had at least 1642 characters on its first line.

impossible to identify exactly which module.exports function was the cause ?

You already have that information: in
module.exports._errs.push({'lib':id,'err':err.message}),
id is the export that caused the problem.

Aside from that, it really depends on what's in the files you're evaling. If there are anonymous functions in there, then of course they'll show up as "anonymous", and you'd have to fix that in there by providing names for all functions.

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