From ceb3e7581d1c57d43e38d913eaead8526540f6df Mon Sep 17 00:00:00 2001 From: Taylor Hakes Date: Sun, 24 Dec 2017 09:02:27 -0500 Subject: [PATCH] fix: Use JSON.stringify for [object Object] --- src/raven.js | 12 ++++++++++-- test/raven.test.js | 29 +++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/raven.js b/src/raven.js index 13353108cc01..3a213e1c8e34 100644 --- a/src/raven.js +++ b/src/raven.js @@ -455,12 +455,20 @@ Raven.prototype = { * @return {Raven} */ captureMessage: function(msg, options) { + // Convert the message into a string + var exceptionMessage; + if (typeof msg === 'object' && msg !== null) { + exceptionMessage = JSON.stringify(msg); + } else { + exceptionMessage = '' + msg; + } + // config() automagically converts ignoreErrors from a list to a RegExp so we need to test for an // early call; we'll error on the side of logging anything called before configuration since it's // probably something you should see: if ( !!this._globalOptions.ignoreErrors.test && - this._globalOptions.ignoreErrors.test(msg) + this._globalOptions.ignoreErrors.test(exceptionMessage) ) { return; } @@ -469,7 +477,7 @@ Raven.prototype = { var data = objectMerge( { - message: msg + '' // Make sure it's actually a string + message: exceptionMessage }, options ); diff --git a/test/raven.test.js b/test/raven.test.js index 8f2393f001d8..7bd3a831f88e 100644 --- a/test/raven.test.js +++ b/test/raven.test.js @@ -2835,15 +2835,36 @@ describe('Raven (public API)', function() { } ]); }); - - it('should coerce message to a string', function() { + it('should coerce object message to a json string', function() { + this.sinon.stub(Raven, 'isSetup').returns(true); + this.sinon.stub(Raven, '_send'); + Raven.captureMessage({foo: 'bar'}); + assert.isTrue(Raven._send.called); + assert.deepEqual(Raven._send.lastCall.args, [ + { + message: '{"foo":"bar"}' + } + ]); + }); + it('should coerce array message to a json string', function() { + this.sinon.stub(Raven, 'isSetup').returns(true); + this.sinon.stub(Raven, '_send'); + Raven.captureMessage([1, 2, 3]); + assert.isTrue(Raven._send.called); + assert.deepEqual(Raven._send.lastCall.args, [ + { + message: '[1,2,3]' + } + ]); + }); + it('should coerce number to a string', function() { this.sinon.stub(Raven, 'isSetup').returns(true); this.sinon.stub(Raven, '_send'); - Raven.captureMessage({}); + Raven.captureMessage(56); assert.isTrue(Raven._send.called); assert.deepEqual(Raven._send.lastCall.args, [ { - message: '[object Object]' + message: '56' } ]); });