Skip to content

Instantly share code, notes, and snippets.

@UberMouse
Created July 3, 2017 21:08
Show Gist options
  • Save UberMouse/286fe6fde4b50412fed5a565a53e5ab2 to your computer and use it in GitHub Desktop.
Save UberMouse/286fe6fde4b50412fed5a565a53e5ab2 to your computer and use it in GitHub Desktop.
Redux exception logging with Raygun4JS
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;
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