From 61045ddd7ff5741523cd42e6525cdfd07c6b67c0 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Sat, 10 Oct 2015 22:41:07 +0100 Subject: [PATCH] try to fix debug node non-standard object type handling. --- nodes/core/core/58-debug.js | 16 ++++++++-------- test/nodes/core/core/58-debug_spec.js | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/nodes/core/core/58-debug.js b/nodes/core/core/58-debug.js index 43e030f59..59eb47e64 100644 --- a/nodes/core/core/58-debug.js +++ b/nodes/core/core/58-debug.js @@ -86,15 +86,12 @@ module.exports = function(RED) { msg.msg = msg.msg.toString('hex'); } else if (typeof msg.msg === 'object') { var seen = []; - msg.format = "object"; + msg.format = msg.msg.constructor.name || "Object"; var isArray = util.isArray(msg.msg); - if (!isArray && msg.msg.toString !== Object.prototype.toString) { - msg.format = msg.msg.constructor.name || "object"; - msg.msg = msg.msg.toString(); - } else { - if (isArray) { - msg.format = "array ["+msg.msg.length+"]"; - } + if (isArray) { + msg.format = "array ["+msg.msg.length+"]"; + } + if (isArray || (msg.format === "Object")) { msg.msg = JSON.stringify(msg.msg, function(key, value) { if (typeof value === 'object' && value !== null) { if (seen.indexOf(value) !== -1) { return "[circular]"; } @@ -102,6 +99,9 @@ module.exports = function(RED) { } return value; }," "); + } else { + try { msg.msg = msg.msg.toString(); } + catch(e) { msg.msg = "[Type not printable]"; } } seen = null; } else if (typeof msg.msg === "boolean") { diff --git a/test/nodes/core/core/58-debug_spec.js b/test/nodes/core/core/58-debug_spec.js index 9c76f9cfc..244316021 100644 --- a/test/nodes/core/core/58-debug_spec.js +++ b/test/nodes/core/core/58-debug_spec.js @@ -94,7 +94,7 @@ describe('debug node', function() { }, function(msg) { JSON.parse(msg).should.eql({ topic:"debug", - data:{id:"n1",msg:'{\n "payload": "test"\n}',format:"object"} + data:{id:"n1",msg:'{\n "payload": "test"\n}',format:"Object"} }); }, done); }); @@ -179,7 +179,7 @@ describe('debug node', function() { }, function(msg) { JSON.parse(msg).should.eql({ topic:"debug", - data:{id:"n1",msg:'{\n "type": "foo"\n}',property:"payload",format:"object"} + data:{id:"n1",msg:'{\n "type": "foo"\n}',property:"payload",format:"Object"} }); }, done); }); @@ -215,7 +215,7 @@ describe('debug node', function() { data:{ id:"n1", msg:'{\n "name": "bar",\n "o": "[circular]"\n}', - property:"payload",format:"object" + property:"payload",format:"Object" } }); }, done);