From 9fbbc386472eff6293ec524df118d0caf8ee5d6c Mon Sep 17 00:00:00 2001 From: Mike Nason Date: Sat, 3 Mar 2018 12:43:35 -0500 Subject: [PATCH 1/2] Dispatch failure FSA when fetch fails Previously: RSAA -> Request FSA -> Request Error FSA Now: RSAA -> Request FSA -> Failure FSA Closes #26 #44 #99 --- README.md | 8 ++++++-- src/middleware.js | 2 +- test/index.js | 41 +++++++++++++++++++---------------------- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 8d8055b..475e269 100644 --- a/README.md +++ b/README.md @@ -366,8 +366,8 @@ The `[RSAA].types` property controls the output of `redux-api-middleware`. The s - `fetch` may throw an error: the RSAA definition is not strong enough to preclude that from happening (you may, for example, send in a `[RSAA].body` that is not valid according to the fetch specification — mind the SHOULDs in the [RSAA definition](#redux-standard-api-calling-actions)); - a network failure occurs (the network is unreachable, the server responds with an error,...). - If such an error occurs, a different *request* FSA will be dispatched (*instead* of the one described above). It will contain the following properties: - - `type`: the string constant in the first position of the `[RSAA].types` array; + If such an error occurs, a *failure* FSA will be dispatched containing the following properties: + - `type`: the string constant in the last position of the `[RSAA].types` array; - `payload`: a [`RequestError`](#requesterror) object containing an error message; - `error: true`. @@ -666,6 +666,7 @@ For example, if you want the status code and status message of a unsuccessful AP } } ``` + By default, *failure* FSAs will not contain a `meta` property, while their `payload` property will be evaluated from ```js (action, state, res) => @@ -674,6 +675,9 @@ By default, *failure* FSAs will not contain a `meta` property, while their `payl ) ``` + +Note that *failure* FSAs dispatched due to fetch errors will not have a `res` argument into `meta` or `payload`. The `res` parameter will exist for completed requests that have resulted in errors, but not for failed requests. + ### Exports The following objects are exported by `redux-api-middleware`. diff --git a/src/middleware.js b/src/middleware.js index d9b1a89..da00c18 100644 --- a/src/middleware.js +++ b/src/middleware.js @@ -166,7 +166,7 @@ function apiMiddleware({ getState }) { return next( await actionWith( { - ...requestType, + ...failureType, payload: new RequestError(e.message), error: true }, diff --git a/test/index.js b/test/index.js index f869b0e..daea496 100644 --- a/test/index.js +++ b/test/index.js @@ -1171,7 +1171,7 @@ test('apiMiddleware must dispatch an error request FSA when [RSAA].options fails actionHandler(anAction); }); -test('apiMiddleware must dispatch an error request FSA on a request error', t => { +test('apiMiddleware must dispatch an failure FSA with an error on a request error', t => { const anAction = { [RSAA]: { endpoint: 'http://127.0.0.1/api/users/1', // We haven't mocked this @@ -1192,7 +1192,9 @@ test('apiMiddleware must dispatch an error request FSA on a request error', t => const doNext = action => { switch (action.type) { case 'REQUEST': - if (!action.error) { + if (action.error) { + t.fail('Request FSA should not have an error'); + } else { t.pass('next handler called'); t.equal( action.type, @@ -1213,31 +1215,26 @@ test('apiMiddleware must dispatch an error request FSA on a request error', t => action.error, 'dispatched non-error FSA has correct error property' ); - break; - } else { - t.pass('next handler called'); - t.equal( - action.type, - 'REQUEST', - 'dispatched error FSA has correct type property' - ); - t.equal( - action.payload.name, - 'RequestError', - 'dispatched error FSA has correct payload property' - ); - t.equal( - action.meta, - 'someMeta', - 'dispatched error FSA has correct meta property' - ); - t.ok(action.error, 'dispatched error FSA has correct error property'); } + break; + case 'FAILURE': + t.equal( + action.type, + 'FAILURE', + 'dispatched error FSA has correct type property' + ); + t.equal( + action.payload.name, + 'RequestError', + 'dispatched error FSA has correct payload property' + ); + t.ok(action.error, 'dispatched error FSA has correct error property'); + break; } }; const actionHandler = nextHandler(doNext); - t.plan(10); + t.plan(8); actionHandler(anAction); }); From 96b0691735e2f20a51ec91741fa9a8a295068bb7 Mon Sep 17 00:00:00 2001 From: Mike Nason Date: Sun, 10 Jun 2018 12:46:18 -0400 Subject: [PATCH 2/2] Update upgrading from 2.x section of readme --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 475e269..c982c99 100644 --- a/README.md +++ b/README.md @@ -902,6 +902,9 @@ $ npm install && npm test ## Upgrading from v2.0.x - The `CALL_API` alias has been removed +- Error handling around failed fetches has been updated (#175) + - Previously, a failed `fetch` would dispatch a `REQUEST` FSA followed by another `REQUEST` FSA with an error flag + - Now, a failed `fetch` will dispatch a `REQUEST` FSA followed by a `FAILURE` FSA ## License