Created
July 3, 2017 21:08
-
-
Save UberMouse/286fe6fde4b50412fed5a565a53e5ab2 to your computer and use it in GitHub Desktop.
Redux exception logging with Raygun4JS
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
const errorReportingMiddleware = store => next => action => { | |
try { | |
return next(action); | |
} catch(err) { | |
console.error('Caught an exception!', err); | |
window.rg4js('send', { | |
error: err, | |
customData: { | |
action, | |
state: store.getState() | |
} | |
}); | |
} | |
}; | |
export default errorReportingMiddleware; |
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
import { createStore, applyMiddleware } from 'redux'; | |
import errorReportingMiddleware from './errorReportingMiddleware'; | |
function App(state = 0, action) { | |
// Simulate an error | |
if (state === 5) | |
null.bar | |
switch(action.type) { | |
case 'increment': | |
return state + 1; | |
case 'decrement': | |
return state - 1; | |
} | |
return state; | |
} | |
// Targeted approach, only attach Redux state to errors that happen during Redux actions | |
// Create the store, including the crashReportingMiddleware | |
const store = createStore(App, applyMiddleware(crashReportingMiddleware)); | |
// Global approach, attach Redux state to any error that occurs on the page | |
// Attach a global Raygun4JS custom data handler | |
window.rg4js('withCustomData', () => { | |
const state = store.getState(); | |
return { state }; | |
}); | |
export default store; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment