From 63cc9adeaa98bae3d4ad60270b108d2ada8355a0 Mon Sep 17 00:00:00 2001 From: Simon Hailes Date: Sat, 2 Nov 2019 12:38:40 +0000 Subject: [PATCH] small update to log - if toString() causes exception, then note and use util.inspect instead - prevents log from causing processes to abort through exception. Add tests. --- packages/node_modules/@node-red/util/lib/log.js | 9 +++++++-- test/unit/@node-red/util/lib/log_spec.js | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/node_modules/@node-red/util/lib/log.js b/packages/node_modules/@node-red/util/lib/log.js index 155863802..8d7b84966 100644 --- a/packages/node_modules/@node-red/util/lib/log.js +++ b/packages/node_modules/@node-red/util/lib/log.js @@ -84,9 +84,14 @@ var consoleLogger = function(msg) { util.log("["+levelNames[msg.level]+"] "+(msg.type?"["+msg.type+":"+(msg.name||msg.id)+"] ":"")+msg.msg.stack); } else { var message = msg.msg; - if (typeof message === 'object' && message !== null && message.toString() === '[object Object]' && message.message) { - message = message.message; + try { + if (typeof message === 'object' && message !== null && message.toString() === '[object Object]' && message.message) { + message = message.message; + } + } catch(e){ + message = 'Exception trying to log: '+util.inspect(message); } + util.log("["+levelNames[msg.level]+"] "+(msg.type?"["+msg.type+":"+(msg.name||msg.id)+"] ":"")+message); } } diff --git a/test/unit/@node-red/util/lib/log_spec.js b/test/unit/@node-red/util/lib/log_spec.js index 9a6f1ac28..73df7d620 100644 --- a/test/unit/@node-red/util/lib/log_spec.js +++ b/test/unit/@node-red/util/lib/log_spec.js @@ -224,5 +224,19 @@ describe("@node-red/util/log", function() { }); + it('it can log without exception', function() { + var msg = { + msg: { + mystrangeobj:"hello", + }, + }; + msg.msg.toString = function(){ + throw new Error('Exception in toString - should have been caught'); + } + msg.msg.constructor = { name: "strangeobj" }; + var ret = log.info(msg.msg); + }); + + });