Custom node-red logger- show line numbers

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)

Could someone please help?

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.

3 Likes

Thanks for sharing, that is really useful to know - I hadn't thought of doing that :thinking: