From 74d760a46d395e77a4991a9d9843e6f1b5f63dd5 Mon Sep 17 00:00:00 2001 From: Simon Hailes Date: Sat, 2 Nov 2019 12:37:07 +0000 Subject: [PATCH] add util.inspect data to 'type not printable'. Add test to cover these lines (no existing test). --- .../node_modules/@node-red/util/lib/util.js | 2 +- test/unit/@node-red/util/lib/util_spec.js | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/packages/node_modules/@node-red/util/lib/util.js b/packages/node_modules/@node-red/util/lib/util.js index df7c404b0..ef43739e6 100644 --- a/packages/node_modules/@node-red/util/lib/util.js +++ b/packages/node_modules/@node-red/util/lib/util.js @@ -755,7 +755,7 @@ function encodeObject(msg,opts) { }," "); } else { try { msg.msg = msg.msg.toString(); } - catch(e) { msg.msg = "[Type not printable]"; } + catch(e) { msg.msg = "[Type not printable]" + util.inspect(msg.msg); } } } } else if (msgType === "function") { diff --git a/test/unit/@node-red/util/lib/util_spec.js b/test/unit/@node-red/util/lib/util_spec.js index 603f4524c..b3f9fe57d 100644 --- a/test/unit/@node-red/util/lib/util_spec.js +++ b/test/unit/@node-red/util/lib/util_spec.js @@ -822,6 +822,53 @@ describe("@node-red/util/util", function() { success.should.eql(1); done(); }); + it('very large object which fails to serialise should be truncated', function(done) { + var msg = { + msg: { + obj:{ + big:"", + cantserialise:{ + message:'this will not be displayed', + toJSON: function(val) { + throw new Error('this exception should have been caught'); + return 'should not display because we threw first'; + }, + }, + canserialise:{ + message:'this should be displayed', + } + }, + } + }; + + for (var i = 0; i < 1000; i++) { + msg.msg.obj.big += 'some more string '; + } + + var result = util.encodeObject(msg); + result.format.should.eql("error"); + var resultJson = JSON.parse(result.msg); + var success = (resultJson.message.length <= 1000); + success.should.eql(true); + done(); + }); + it('test bad toString', function(done) { + 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 result = util.encodeObject(msg); + var success = (result.msg.indexOf('[Type not printable]') >= 0); + success.should.eql(true); + done(); + }); + }); });