Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User context #49

Merged
merged 3 commits into from
Dec 27, 2017
Merged

User context #49

merged 3 commits into from
Dec 27, 2017

Conversation

odinuge
Copy link
Contributor

@odinuge odinuge commented Dec 27, 2017

Hi,

This adds a way to set the user context with a state transformer. Any thoughts about this? (the name of the new attr is kinda bad, i know). It would be nice to be able to set the user context in the same callback as the state, because most often it only depends on the state. Here is a simple example with https://github.com/reactjs/reselect, and a state transformed with https://github.com/paularmstrong/normalizr:

import { createSelector } from 'reselect';
export const selectCurrentUser = createSelector(
  state => state.users.byId,
  state => state.auth.id,
  (usersById, userId) => usersById[userId]
);
[...]
createRavenMiddleware(Raven, {
            userContextStateTransformer: selectCurrentUser
})

I have also added a few more tests in the second commit (now all lines are covered). If this looks good, i can write documentation so we can get it merged.

Thanks for this project! We use it in https://github.com/webkom/lego-webapp, and it works like a charm. 😄 We also use it for raven (the node version), but that version has a more limited API - eg.setDataCallback is not available.

@codecov-io
Copy link

codecov-io commented Dec 27, 2017

Codecov Report

Merging #49 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master    #49   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           1      1           
  Lines          21     24    +3     
  Branches        8      9    +1     
=====================================
+ Hits           21     24    +3
Impacted Files Coverage Δ
index.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1fd666b...d92db68. Read the comment docs.

Copy link
Owner

@captbaritone captbaritone left a comment

Choose a reason for hiding this comment

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

Looks good! Thanks for suggesting this. Add the docs and i’m happy to merge. FYI I’ll be offline until Saturday.

expect(context.mockTransport.mock.calls[0][0].data.user).toEqual(
userData
);
});
Copy link
Owner

Choose a reason for hiding this comment

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

I don’t think this test is really nessesary. The first one like this, in the “default config” test block is probably enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the "duplicate" test now

index.js Outdated
@@ -8,15 +8,20 @@ function createRavenMiddleware(Raven, options = {}) {
actionTransformer = identity,
stateTransformer = identity,
breadcrumbCategory = "redux-action",
filterBreadcrumbActions = filter
filterBreadcrumbActions = filter,
userContextStateTransformer
Copy link
Owner

Choose a reason for hiding this comment

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

Maybe we could call it getUserContext? That would fit with the convention i’ve seen for naming selectors which, as you show in your example, this is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree! That is a much better name!

@odinuge
Copy link
Contributor Author

odinuge commented Dec 27, 2017

That was quick!

Sounds good @captbaritone; have added some initial documentation now.

README.md Outdated

Default: `undefined` (disabled)

Signature: `state => userContext`
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if this is the best way to do this, and still be consistent with the others. Since it cannot default to state => undefined; because it should be possible not to use it, and use the Raven.setUserContext elsewhere....

Copy link
Owner

Choose a reason for hiding this comment

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

How about we specify the signature as:

#### `getUserContext` _(Optional Function)_

And then omit the “default” line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good 👍 Fixed it now 😄

data.extra.state = stateTransformer(store.getState());
data.extra.state = stateTransformer(state);
if (getUserContext) {
data.user = getUserContext(state);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you think we should add a note that the original state, and not the transformed one?

Copy link
Owner

Choose a reason for hiding this comment

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

You mean, a note about it overwriting any existing user context?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, but I did indeed add a notice about that.

But, a note saying that the getUserContext is executed with the original redux-state, and not the state transformed via the stateTransformer.

Copy link
Owner

Choose a reason for hiding this comment

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

In the docs, as long as we refer to it as “Redux state” I think that’s clear. In the code, I think it’s self explanatory.

README.md Outdated
Signature: `state => userContext`

The [user context] is an important part of the error reporting. When
`getUserContext` is a truthy function, the result of `getUserContext`
Copy link
Owner

Choose a reason for hiding this comment

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

All functions are truthy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah. When setting the function to optional, using truthy doesn't make that much sense.

Copy link
Owner

Choose a reason for hiding this comment

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

Maybe just “when getUserContext is specified”?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍
Did a small rephrase now 😄

README.md Outdated

The [user context] is an important part of the error reporting. When
`getUserContext` is a truthy function, the result of `getUserContext`
will set the the [user context] before sending the error report.
Copy link
Owner

Choose a reason for hiding this comment

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

I think it’s standard to only make the first reference to a given term a link.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

Copy link
Owner

@captbaritone captbaritone left a comment

Choose a reason for hiding this comment

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

Looks good to me! I’ll cut a new release when I get back on the grid this weekend. Thanks for your quick work on this! 👏

@captbaritone captbaritone merged commit 49f4d15 into captbaritone:master Dec 27, 2017
@captbaritone
Copy link
Owner

Published in 1.2.0. Thanks for the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants