So basically I am dispatching an action with thunk and redux-promise-middleware, which makes an API call that returns a promise. I then send the promise returned to another action creator as a 'payload' argument, which works with the redux-promise-middleware and handles different actions MY_ACTION_TYPE_PENDING or MY_ACTION_TYPE_REJECTED or MY_ACTION_TYPE_FULFILLED. My question is do I handle the errors in reducer via the _REJECTED action and not catch it on my dispatch(actionCreator(payload)? When I do not catch the error on my dispatch I get a warning in the console, despite my reducer handling the error well with the _REJECTED ACTION.
Below are some of my actions:
export const RECEIVE_POSTS = 'RECEIVE_POSTS';
export const receivePosts = (data) => ({
type: RECEIVE_POSTS,
payload: data
})
// thunk middleware for fetching blog
export const fetchPosts = () => {
return (dispatch) => {
const payload = contentfulClient.getEntries().then(
data => data.items,
error => console.log('An error occurred in fetchPost thunk middleware', error)
)
return dispatch(receivePosts(payload))
.catch((error) => {
console.log('caught error in fetchPost', error)
})
}
}
then this is some of my blog reducers file, it handles the actions the promise middleware sends out
const status = (state = Status.IDLE, action) => {
switch (action.type) {
case `${RECEIVE_POSTS}_PENDING` :
return Status.PENDING;
case `${RECEIVE_POSTS}_FULFILLED`:
return Status.FULFILLED;
case `${RECEIVE_POSTS}_REJECTED`:
return Status.REJECTED;
default:
return state
}
}
const error = (state = null, action) => {
switch (action.type) {
case `${RECEIVE_POSTS}_REJECTED`:
return action.payload.message
default:
return state;
}
}