Skip to content

Commit df8a9cd

Browse files
authored
fix: create safeJoin util for console wrappers (#1222)
1 parent b532909 commit df8a9cd

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

src/console.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var utils = require('./utils');
2+
13
var wrapMethod = function(console, level, callback) {
24
var originalConsoleLevel = console[level];
35
var originalConsole = console;
@@ -11,13 +13,14 @@ var wrapMethod = function(console, level, callback) {
1113
console[level] = function() {
1214
var args = [].slice.call(arguments);
1315

14-
var msg = '' + args.join(' ');
16+
var msg = utils.safeJoin(args, ' ');
1517
var data = {level: sentryLevel, logger: 'console', extra: {arguments: args}};
1618

1719
if (level === 'assert') {
1820
if (args[0] === false) {
1921
// Default browsers message
20-
msg = 'Assertion failed: ' + (args.slice(1).join(' ') || 'console.assert');
22+
msg =
23+
'Assertion failed: ' + (utils.safeJoin(args.slice(1), ' ') || 'console.assert');
2124
data.extra.arguments = args.slice(1);
2225
callback && callback(msg, data);
2326
}

src/utils.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,28 @@ function fill(obj, name, replacement, track) {
397397
}
398398
}
399399

400+
/**
401+
* Join values in array
402+
* @param input array of values to be joined together
403+
* @param delimiter string to be placed in-between values
404+
* @returns {string}
405+
*/
406+
function safeJoin(input, delimiter) {
407+
if (!isArray(input)) return '';
408+
409+
var output = [];
410+
411+
for (var i = 0; i < input.length; i++) {
412+
try {
413+
output.push(String(input[i]));
414+
} catch (e) {
415+
output.push('[value cannot be serialized]');
416+
}
417+
}
418+
419+
return output.join(delimiter);
420+
}
421+
400422
module.exports = {
401423
isObject: isObject,
402424
isError: isError,
@@ -423,5 +445,6 @@ module.exports = {
423445
isSameException: isSameException,
424446
isSameStacktrace: isSameStacktrace,
425447
parseUrl: parseUrl,
426-
fill: fill
448+
fill: fill,
449+
safeJoin: safeJoin
427450
};

test/utils.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var urlencode = utils.urlencode;
2323
var htmlTreeAsString = utils.htmlTreeAsString;
2424
var htmlElementAsString = utils.htmlElementAsString;
2525
var parseUrl = utils.parseUrl;
26+
var safeJoin = utils.safeJoin;
2627

2728
describe('utils', function() {
2829
describe('isUndefined', function() {
@@ -421,4 +422,35 @@ describe('utils', function() {
421422
);
422423
});
423424
});
425+
426+
describe('safeJoin', function() {
427+
it('should return empty string if not-array input provided', function() {
428+
assert.equal(safeJoin('asd'), '');
429+
assert.equal(safeJoin(undefined), '');
430+
assert.equal(safeJoin({foo: 123}), '');
431+
});
432+
433+
it('should default to comma, as regular join() call', function() {
434+
assert.equal(safeJoin(['a', 'b', 'c']), 'a,b,c');
435+
});
436+
437+
it('should stringify complex values, as regular String() call', function() {
438+
assert.equal(
439+
safeJoin([1, 'a', {foo: 42}, [1, 2, 3]], ' '),
440+
'1 a [object Object] 1,2,3'
441+
);
442+
});
443+
444+
it('should still work with unserializeable values', function() {
445+
function Foo() {}
446+
Foo.prototype.toString = function() {
447+
throw Error('whoops!');
448+
};
449+
450+
assert.equal(
451+
safeJoin([new Foo(), 'abc', new Foo(), 42], ' X '),
452+
'[value cannot be serialized] X abc X [value cannot be serialized] X 42'
453+
);
454+
});
455+
});
424456
});

0 commit comments

Comments
 (0)