Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

fix: Do not override context when capturing non-error exceptions #444

Closed
wants to merge 1 commit into from

Conversation

kamilogorek
Copy link
Contributor

Resolves #442

@kamilogorek kamilogorek requested a review from a team April 4, 2018 10:20
@@ -331,7 +331,8 @@ extend(Raven.prototype, {
cb = kwargs;
kwargs = {};
} else {
kwargs = kwargs || {};
// Do not use reference, as we'll modify this object later on
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you need to do this one too? because of line 353?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because, we don't want to mutate someone else's objects.

var extraInfo = {
  extra: {
    something: 'fromDev'
  }
}

function foo () {
  try {
    // something, something
  } catch (e) {
    Raven.captureException(e, extraInfo);
  }
}

function bar () {
  try {
    // something else
  } catch (e) {
    Raven.captureMessage('something broke here', extraInfo);
  }
}

foo();
bar(); // whoops, this will already be modified `foo` call

I also wanted to be consistent across captureException/captureMessage.

Copy link
Contributor

@MaxBittker MaxBittker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice turnaround time on the issue!

left some nits, but i think this looks good! my main concern is putting JSON.parse(stringify(kwargs))
on the critical path of all sentry errors - will that cause problems if people are currently putting weird stuff there?

@@ -367,25 +368,24 @@ extend(Raven.prototype, {
cb = kwargs;
kwargs = {};
} else {
kwargs = kwargs || {};
// Do not use reference, as we'll modify this object later on
kwargs = kwargs ? JSON.parse(stringify(kwargs)) : {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there anything not serializable that we should be worried about being passed here, like functions?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you consider only copying this at the point of mutation to avoid this code running on all execution paths?

not sure if that's actually a good idea or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we may run into issues with req object from some frameworks... Although, I'm not sure why we even pass it down to errorHandler if it's already covered by adding req/res to context in requestHandler which is used in all our framework docs - https://github.com/getsentry/raven-node/blob/master/lib/client.js#L540-L568

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read through the codebase once again and it appears that we should be perfectly fine.

https://github.com/getsentry/raven-node/blob/master/lib/client.js#L549-L562

This middleware is passing just an err itself, so we only have to preserve req just in case someone wants to use it inside dataCallback.

b5e306c#diff-50cfa59973c04321b5da0c6da0fdf4feR367

kwargs.extra = {
__serialized__: serializedException
};
kwargs = extend(kwargs, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

client.send = function mockSend(kwargs) {
client.send = old;
kwargs.extra.should.have.property('hello', 'there');
kwargs.extra.should.not.equal(info);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this test be should.not.equal(info.extra)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this test should assert on the shape of info.extra anyway to be doubly sure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to check the reference of a whole object that was passed to capture calls as the second argument.
We have a shape-based tests like this one https://github.com/getsentry/raven-node/pull/444/files#diff-c953753f049964fe98760ee9244e055cR341

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think line 430 is a false positive and it should be should.not.equal(info.extra)

(this test in its current form passes without your change)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're totally right. Fixed.

@@ -367,25 +368,24 @@ extend(Raven.prototype, {
cb = kwargs;
kwargs = {};
} else {
kwargs = kwargs || {};
// Do not use reference, as we'll modify this object later on
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a normal way to accomplish a multiple arity function with an optional second arg? it seems a bit complicated but it's worked well so far

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately yes :(

@kamilogorek
Copy link
Contributor Author

Merged manually

@kamilogorek kamilogorek closed this Apr 9, 2018
@kamilogorek kamilogorek deleted the preserve-context branch April 9, 2018 12:54
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants