Created
June 8, 2016 07:53
-
-
Save IrakliJani/dfdc7a3275a870278a1a0ee2a5a180db to your computer and use it in GitHub Desktop.
This file contains 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 { 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