Skip to content

Instantly share code, notes, and snippets.

@IrakliJani
Created June 8, 2016 07:53
Show Gist options
  • Save IrakliJani/dfdc7a3275a870278a1a0ee2a5a180db to your computer and use it in GitHub Desktop.
Save IrakliJani/dfdc7a3275a870278a1a0ee2a5a180db to your computer and use it in GitHub Desktop.
import { applyMiddleware } from 'redux'
import { addAlert } from 'actions/Alert'
function errorHandler () {
return (next) => (action) => {
if (typeof action === 'function') {
return next(async function (dispatch, getState) {
try {
return await action(dispatch, getState)
} catch (error) {
if (error && error.data || error.statusText) {
return dispatch(addAlert('error', error.data.errors || error.statusText))
} else {
throw error
}
}
})
} else {
return next(action)
}
}
}
export default () => applyMiddleware(errorHandler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment