Skip to content

don't call FETCH_SUCCESS on error #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@ export default store => {

rpc
.call(action.name, args)
.then(result =>
store.dispatch(
actions.fetchSuccess({
name: action.name,
args,
id,
result,
}),
),
)
.then(result => {
console.log(result)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove this debugging line? If you find this logging useful for you, maybe add an option to the middleware like { debug: true }?


if (result) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious, what is the response code in this case? My only question is, should this be handled in the RPCClient library and not here? If you think this should be handled higher up in the client library, that might be good to ship a change there first, but that's your call. If you want to bounce an idea around for that, I know Eduardo and Phil have thought a bunch about the RPC server side code and the client itself and we haven't iterated on the client library since Harrison wrote it last year :D

store.dispatch(
actions.fetchSuccess({
name: action.name,
args,
id,
result,
}),
)
}
})
.catch(error => {
console.error(error) // eslint-disable-line
store.dispatch(
Expand Down
27 changes: 27 additions & 0 deletions test/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,31 @@ describe('middleware', () => {
error: RPCClient.fakeError,
})
})
it('should not dispatch a FETCH_SUCCESS action on error', async () => {
const store = {
dispatch: jest.fn(),
getState: () => ({
asyncDataFetch: {},
}),
}
const name = 'fail'
const args = {
test: 'yes',
}
const action = {
type: actionTypes.FETCH,
name,
args,
}
middleware(store)(() => {})(action)
await sleep() // give the event loop a chance to process promise
expect(store.dispatch).toHaveBeenCalledTimes(2)
expect(store.dispatch).toHaveBeenLastCalledWith({
type: `${name}_${actionTypes.FETCH_FAIL}`,
name,
args,
id: 0,
error: RPCClient.fakeError,
})
})
})