Created
June 25, 2020 21:23
-
-
Save jennmueng/d189deca6d0d25779dc5702e2412ee48 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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