Skip to content

Commit 170a1a2

Browse files
authored
fix: shallow-clone context instead of deep-copy (#452)
1 parent b1f37bc commit 170a1a2

File tree

2 files changed

+5
-69
lines changed

2 files changed

+5
-69
lines changed

lib/client.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,7 @@ extend(Raven.prototype, {
333333
cb = kwargs;
334334
kwargs = {};
335335
} else {
336-
// Do not use reference, as we'll modify this object later on
337-
kwargs = kwargs ? JSON.parse(stringify(kwargs)) : {};
336+
kwargs = utils.isPlainObject(kwargs) ? extend({}, kwargs) : {};
338337
}
339338

340339
var eventId = this.generateEventId();
@@ -364,12 +363,7 @@ extend(Raven.prototype, {
364363
cb = kwargs;
365364
kwargs = {};
366365
} else {
367-
var req = kwargs && kwargs.req;
368-
// Do not use reference, as we'll modify this object later on
369-
kwargs = kwargs ? JSON.parse(stringify(kwargs)) : {};
370-
// Preserve `req` so we can use some framework's middleware methods
371-
// `req` is the only thing that we pass through in `errorHandler`, so we need just that
372-
if (req) kwargs.req = req;
366+
kwargs = utils.isPlainObject(kwargs) ? extend({}, kwargs) : {};
373367
}
374368

375369
if (!(err instanceof Error)) {

test/raven.client.js

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -223,22 +223,6 @@ describe('raven.Client', function() {
223223
done();
224224
});
225225
});
226-
227-
it('should copy object with extra data instead of using its reference directly', function(done) {
228-
var old = client.send;
229-
var info = {
230-
extra: {
231-
hello: 'there'
232-
}
233-
};
234-
client.send = function mockSend(kwargs) {
235-
client.send = old;
236-
kwargs.extra.should.have.property('hello', 'there');
237-
kwargs.extra.should.not.equal(info);
238-
done();
239-
};
240-
client.captureMessage('exception', info);
241-
});
242226
});
243227

244228
describe('#captureException()', function() {
@@ -343,12 +327,9 @@ describe('raven.Client', function() {
343327
var old = zlib.deflate;
344328
zlib.deflate = function mockSend(skwargs) {
345329
zlib.deflate = old;
346-
347330
var kwargs = JSON.parse(skwargs);
348-
// Remove superfluous node version data to simplify the test itself
349-
delete kwargs.extra.node;
350-
kwargs.should.have.property('extra', {
351-
foo: '[Circular ~]'
331+
kwargs.extra.should.have.property('foo', {
332+
foo: '[Circular ~.extra.foo]'
352333
});
353334
done();
354335
};
@@ -359,7 +340,7 @@ describe('raven.Client', function() {
359340
foo: null
360341
}
361342
};
362-
kwargs.extra.foo = kwargs;
343+
kwargs.extra.foo = kwargs.extra;
363344
client.captureException(new Error('wtf?'), kwargs);
364345
});
365346

@@ -416,45 +397,6 @@ describe('raven.Client', function() {
416397
}
417398
);
418399
});
419-
420-
it('should copy object with extra data instead of using its reference directly', function(done) {
421-
var old = client.send;
422-
var info = {
423-
extra: {
424-
hello: 'there'
425-
}
426-
};
427-
client.send = function mockSend(kwargs) {
428-
client.send = old;
429-
kwargs.extra.should.have.property('hello', 'there');
430-
kwargs.extra.should.not.equal(info.extra);
431-
done();
432-
};
433-
client.captureException({some: 'exception'}, info);
434-
});
435-
436-
it('should preserve same reference to `req` attribute in kwargs', function(done) {
437-
var old = client.process;
438-
var info = {
439-
extra: {
440-
hello: 'there'
441-
},
442-
req: {
443-
something: 'else'
444-
}
445-
};
446-
// Use `process` instead of `send` as `req` is stripped from the final payload
447-
client.process = function mockProcess(id, kwargs) {
448-
client.process = old;
449-
kwargs.extra.should.have.property('hello', 'there');
450-
kwargs.extra.should.not.equal(info.extra);
451-
452-
kwargs.req.should.have.property('something', 'else');
453-
kwargs.req.should.equal(info.req);
454-
done();
455-
};
456-
client.captureException({some: 'exception'}, info);
457-
});
458400
});
459401

460402
describe('#install()', function() {

0 commit comments

Comments
 (0)