Hi
I am trying to debug an error in my custom node. I get something like this in the console:
14 Nov 16:28:44 - [error] [test-validation:d5746feb.8c06d] TypeError: Cannot read property 'undefined' of
undefined
Though this gives me the name of the node and the node id, it does not give the line number where the error occured. I tried adding a custom logger as below in the settings.js file under logging:
// Custom logger
myCustomLogger: {
level: 'error',
metrics: false,
handler: function(settings) {
// Called when the logger is initialised
// Return the logging function
return function(msg) {
console.trace('custom logger', msg.timestamp, msg.event);
}
}
}
. However thsi still does not give me the line number in the node where the error occured:
Trace: custom logger 1573709324619 undefined
at LogHandler.handler (C:\Users\umasudhan.node-red\settings.js:270:25)
at LogHandler. (C:\Users\umasudhan\AppData\Roaming\npm\node_modules\node-red\node_modules@node-red\util\lib\log.js:67:18)
at LogHandler.emit (events.js:189:13)
at C:\Users\umasudhan\AppData\Roaming\npm\node_modules\node-red\node_modules@node-red\util\lib\log.js:156:21
at Array.forEach ()
at Object.log (C:\Users\umasudhan\AppData\Roaming\npm\node_modules\node-red\node_modules@node-red\util\lib\log.js:155:21)
at Object.createNode (C:\Users\umasudhan\AppData\Roaming\npm\node_modules\node-red\node_modules@node-red\runtime\lib\nodes\flows\util.js:485:25)
at Flow.start (C:\Users\umasudhan\AppData\Roaming\npm\node_modules\node-red\node_modules@node-red\runtime\lib\nodes\flows\Flow.js:176:44)
at start (C:\Users\umasudhan\AppData\Roaming\npm\node_modules\node-red\node_modules@node-red\runtime\lib\nodes\flows\index.js:329:33)
at tryCatchReject (C:\Users\umasudhan\AppData\Roaming\npm\node_modules\node-red\node_modules\when\lib\makePromise.js:845:30)
Sometimes, the complex structure of Node-RED doesn't allow it to know what line created the issue. This has been discussed before in the forum so if you want the details, you'll need to go searching.
Bottom line is that it isn't always possible for Node-RED to know what line in an originating node triggered the problem.
If that error comes from a function node, you can add node.warn messages to look at your variables/properties at specific points in order to work out what is wrong. A mistyped variable name or something happening out of the expected order or maybe the source data isn't quite what you expected. All of these might be common reasons for what you are seeing.
If it is coming from some other node, that might be harder to track down and you might need to raise an issue with the node's author. Or dig into the source code and add some custom console output.
Thanks again Julian. I finally found a way that seemed to work for me- adding a catch node with a debug out seems to output the stack trace with line numbers.
Sorry guys, misunderstood: I thought this is about getting a line number for an error within a function node...
Actually, adding a catch node did not help me but wrapping the code in a try catch block and logging the error did. Tested with this code:
//
try{
var b=null;
//
//
var a=b.a;
//
//
}
catch(e){
node.warn(e.stack);
}
return msg;
This will log a message like:
TypeError: Cannot read property 'a' of null at Function node:4f5f2b5e.37917c:7:9 at Function node:4f5f2b5e.37917c:15:3 at Script.runInContext (vm.js:131:20) at processMessage (/usr/local/lib/node_modules/node-red/node_modules/@node-red/nodes/core/function/10-function.js:312:29) at FunctionNode._inputCallback (/usr/local/lib/node_modules/node-red/node_modules/@node-red/nodes/core/function/10-function.js:377:21) at /usr/local/lib/node_modules/node-red/node_modules/@node-red/runtime/lib/nodes/Node.js:203:26 at Object.trigger (/usr/local/lib/node_modules/node-red/node_modules/@node-red/runtime/lib/hooks.js:113:9) at FunctionNode.Node._emitInput (/usr/local/lib/node_modules/node-red/node_modules/@node-red/runtime/lib/nodes/Node.js:195:11) at FunctionNode.Node.emit (/usr/local/lib/node_modules/node-red/node_modules/@node-red/runtime/lib/nodes/Node.js:179:25) at FunctionNode.Node.receive (/usr/local/lib/node_modules/node-red/node_modules/@node-red/runti...
As you can see, the message begins with:
TypeError: Cannot read property 'a' of null at Function node:4f5f2b5e.37917c:7:9
where 7:9 indicates the line:column but to whatever reasons the line number is indicates the next line (7) but the error here happens at line 6. Note that some said on certain forums (stackoverflow) that the e.stack property is browser dependent (I tried it in FF).
Thanks for the info! I simply played around with adding commented lines here and there to see if it's always n-1 and after a while I concluded that it's reliable