改进动态构建模块的错误报告。
我有一个应用程序,可以将单个较小的文件动态加载到模块中。运行时,它的运行良好已有几年了。
这是迭代文件夹,读取它们的代码的主要部分,然后将它们附加到所需的模块上。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
,
函数(err,code){...}
是一个匿名函数,函数notanymon(err,code) {...}
不是。请参阅此示例节点会话:请注意第二版和第三版本如何包含摘要
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的文件中的内容。如果其中有匿名功能,那么它们当然会显示为“匿名”,并且您必须通过提供所有功能的名称来解决此处。Well,
function(err,code){...}
is an anonymous function,function NotAnonymous(err, code){...}
is not. See this example Node session: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 geteval
ed don't have a file name. To get change that, you'd have to stop usingeval
. 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.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
eval
ing. 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.