Skip to content

Instantly share code, notes, and snippets.

@akhayoon
Created September 20, 2017 20:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akhayoon/24be6ca9634d228893b8c4bd79f0f33c to your computer and use it in GitHub Desktop.
Save akhayoon/24be6ca9634d228893b8c4bd79f0f33c to your computer and use it in GitHub Desktop.
import Raven from 'raven-js'
// Custom Redux-Thunk that catches errors
function createThunkMiddleware(extraArgument) {
return ({ dispatch, getState }) => next => action =>
{
if (typeof action === 'function')
{
const result = action(dispatch, getState, extraArgument);
// If the function returns a promise (async function usually), then
// catch the error manually and report to Raven.
if (result && result.catch && typeof(result.catch) === 'function')
{
return result.catch((error) =>
{
console.error(error);
Raven.captureException(error);
});
}
return result;
}
return next(action);
}
}
const enhancedReduxThunk = createThunkMiddleware();
enhancedReduxThunk.withExtraArgument = createThunkMiddleware;
export default enhancedReduxThunk;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment