Skip to content

Instantly share code, notes, and snippets.

@olavoasantos
Last active November 16, 2019 20:41
Show Gist options
  • Save olavoasantos/9ac791098758ee7dedf0c0424ec8b398 to your computer and use it in GitHub Desktop.
Save olavoasantos/9ac791098758ee7dedf0c0424ec8b398 to your computer and use it in GitHub Desktop.
import { omit } from 'lodash';
const filter = () => true;
const getUndefined = () => {};
const getType = (action) => action.type;
const identity = (x) => x;
const createSentryMiddleware = (Sentry, options = {}) => {
const {
actionTransformer = identity,
breadcrumbCategory = 'redux-action',
breadcrumbDataFromAction = getUndefined,
breadcrumbMessageFromAction = getType,
filterBreadcrumbActions = filter,
getTags,
getUserContext,
stateTransformer = identity,
} = options;
let lastAction;
let lastEventId;
let capturedEvents = {};
return (store) => {
Sentry.configureScope((scope) => {
scope.addEventProcessor((event) => {
const state = store.getState();
// If the user getter is defined, we
// should add it to the current
// context.
if (getUserContext) {
scope.setUser(getUserContext(state));
}
// If the tags getter is defined, we
// should add it to the current
// context.
if (getTags) {
const tags = getTags(state);
Object.keys(tags).forEach((key) => {
scope.setTag(key, tags[key]);
});
}
// Get the captured event data and remove it from
// the captured events to avoid storing
// excessive data.
const capturedEvent = capturedEvents[event.event_id] || {
extra: {
state: stateTransformer(state),
lastAction: actionTransformer(lastAction),
},
breadcrumbs: event.breadcrumbs,
};
capturedEvents = omit(capturedEvents, event.event_id);
// Return our modified event which now contains the
// extra data, user, tag and only the breadcrumbs
// added util the error was thrown.
return {
...event,
breadcrumbs: capturedEvent.breadcrumbs,
extra: {
...event.extra,
...capturedEvent.extra,
},
};
});
});
return (next) => (action) => {
if (filterBreadcrumbActions(action)) {
// If there is a new error emmited, we should
// take a snapshot of the current state,
// action and breadcrumbs.
if (Sentry.lastEventId() !== lastEventId) {
const state = store.getState();
lastEventId = Sentry.lastEventId();
capturedEvents[lastEventId] = {
extra: {
state: stateTransformer(state),
lastAction: actionTransformer(lastAction),
},
breadcrumbs: Sentry.getCurrentHub().getStack()[0].scope._breadcrumbs,
};
}
// Add new breadcrumb
Sentry.addBreadcrumb({
category: breadcrumbCategory,
data: breadcrumbDataFromAction(action),
message: breadcrumbMessageFromAction(action),
});
}
lastAction = action;
return next(action);
};
};
};
export default createSentryMiddleware;
@JCMais
Copy link

JCMais commented May 17, 2019

On line 84:

            breadcrumbs: Sentry.getCurrentHub().stack[0].scope.breadcrumbs,

This has changed on latest Sentry version, it should now be:

            breadcrumbs: Sentry.getCurrentHub().getStack()[0].scope._breadcrumbs

@olavoasantos
Copy link
Author

Updated! Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment