Skip to content

Instantly share code, notes, and snippets.

@jennmueng
Created June 25, 2020 21:23
Show Gist options
  • Save jennmueng/d189deca6d0d25779dc5702e2412ee48 to your computer and use it in GitHub Desktop.
Save jennmueng/d189deca6d0d25779dc5702e2412ee48 to your computer and use it in GitHub Desktop.
// @flow
import * as Sentry from '@sentry/react-native';
import type { Store } from 'redux';
type Action = {
type: string
};
type State = {};
type SentryMiddlewareOptions = {
stateProcessor: State => ?State,
actionProcessor: Action => ?Action
};
const defaultOptions = {
stateProcessor: state => state,
actionProcessor: action => action
};
// const defaultStateProcessor = (state: State) => State => {
// const jsonString = JSON.stringify(state);
// const redactableValues = new RegExp('"password":', 'g');
// const redacted = jsonString.replaceAll()
// }
const configureMiddleware = (options: SentryMiddlewareOptions = defaultOptions) => {
const sentryReduxMiddleware = (store: Store) => {
Sentry.addGlobalEventProcessor(event => {
const state = store.getState();
const processedState = options.stateProcessor(state);
if (processedState) {
if (event.extra) {
// eslint-disable-next-line no-param-reassign
event.extra['redux.state'] = processedState;
} else {
// eslint-disable-next-line no-param-reassign
event.extra = {
'redux.state': processedState
};
}
}
return event;
});
return (next: Action => void) => (action: Action) => {
const processedAction = options.actionProcessor(action);
if (typeof processedAction !== 'undefined' && processedAction !== null) {
Sentry.configureScope(scope => {
scope.addBreadcrumb({
type: 'info',
category: 'redux-action',
data: processedAction
});
});
}
return next(action);
};
};
return sentryReduxMiddleware;
};
const REDACTED = '[STATE-REDACTED]';
export { configureMiddleware, REDACTED };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment