Skip to content

Commit 9fbbc38

Browse files
committed
Dispatch failure FSA when fetch fails
Previously: RSAA -> Request FSA -> Request Error FSA Now: RSAA -> Request FSA -> Failure FSA Closes #26 #44 #99
1 parent 836a3d3 commit 9fbbc38

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-25
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,8 @@ The `[RSAA].types` property controls the output of `redux-api-middleware`. The s
366366
- `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));
367367
- a network failure occurs (the network is unreachable, the server responds with an error,...).
368368
369-
If such an error occurs, a different *request* FSA will be dispatched (*instead* of the one described above). It will contain the following properties:
370-
- `type`: the string constant in the first position of the `[RSAA].types` array;
369+
If such an error occurs, a *failure* FSA will be dispatched containing the following properties:
370+
- `type`: the string constant in the last position of the `[RSAA].types` array;
371371
- `payload`: a [`RequestError`](#requesterror) object containing an error message;
372372
- `error: true`.
373373
@@ -666,6 +666,7 @@ For example, if you want the status code and status message of a unsuccessful AP
666666
}
667667
}
668668
```
669+
669670
By default, *failure* FSAs will not contain a `meta` property, while their `payload` property will be evaluated from
670671
```js
671672
(action, state, res) =>
@@ -674,6 +675,9 @@ By default, *failure* FSAs will not contain a `meta` property, while their `payl
674675
)
675676
```
676677
678+
679+
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.
680+
677681
### Exports
678682
679683
The following objects are exported by `redux-api-middleware`.

src/middleware.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ function apiMiddleware({ getState }) {
166166
return next(
167167
await actionWith(
168168
{
169-
...requestType,
169+
...failureType,
170170
payload: new RequestError(e.message),
171171
error: true
172172
},

test/index.js

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ test('apiMiddleware must dispatch an error request FSA when [RSAA].options fails
11711171
actionHandler(anAction);
11721172
});
11731173

1174-
test('apiMiddleware must dispatch an error request FSA on a request error', t => {
1174+
test('apiMiddleware must dispatch an failure FSA with an error on a request error', t => {
11751175
const anAction = {
11761176
[RSAA]: {
11771177
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 =>
11921192
const doNext = action => {
11931193
switch (action.type) {
11941194
case 'REQUEST':
1195-
if (!action.error) {
1195+
if (action.error) {
1196+
t.fail('Request FSA should not have an error');
1197+
} else {
11961198
t.pass('next handler called');
11971199
t.equal(
11981200
action.type,
@@ -1213,31 +1215,26 @@ test('apiMiddleware must dispatch an error request FSA on a request error', t =>
12131215
action.error,
12141216
'dispatched non-error FSA has correct error property'
12151217
);
1216-
break;
1217-
} else {
1218-
t.pass('next handler called');
1219-
t.equal(
1220-
action.type,
1221-
'REQUEST',
1222-
'dispatched error FSA has correct type property'
1223-
);
1224-
t.equal(
1225-
action.payload.name,
1226-
'RequestError',
1227-
'dispatched error FSA has correct payload property'
1228-
);
1229-
t.equal(
1230-
action.meta,
1231-
'someMeta',
1232-
'dispatched error FSA has correct meta property'
1233-
);
1234-
t.ok(action.error, 'dispatched error FSA has correct error property');
12351218
}
1219+
break;
1220+
case 'FAILURE':
1221+
t.equal(
1222+
action.type,
1223+
'FAILURE',
1224+
'dispatched error FSA has correct type property'
1225+
);
1226+
t.equal(
1227+
action.payload.name,
1228+
'RequestError',
1229+
'dispatched error FSA has correct payload property'
1230+
);
1231+
t.ok(action.error, 'dispatched error FSA has correct error property');
1232+
break;
12361233
}
12371234
};
12381235
const actionHandler = nextHandler(doNext);
12391236

1240-
t.plan(10);
1237+
t.plan(8);
12411238
actionHandler(anAction);
12421239
});
12431240

0 commit comments

Comments
 (0)