Skip to content

Commit ebf15ec

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 750524f commit ebf15ec

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
@@ -360,8 +360,8 @@ The `[RSAA].types` property controls the output of `redux-api-middleware`. The s
360360
- `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));
361361
- a network failure occurs (the network is unreachable, the server responds with an error,...).
362362
363-
If such an error occurs, a different *request* FSA will be dispatched (*instead* of the one described above). It will contain the following properties:
364-
- `type`: the string constant in the first position of the `[RSAA].types` array;
363+
If such an error occurs, a *failure* FSA will be dispatched containing the following properties:
364+
- `type`: the string constant in the last position of the `[RSAA].types` array;
365365
- `payload`: a [`RequestError`](#requesterror) object containing an error message;
366366
- `error: true`.
367367
@@ -660,6 +660,7 @@ For example, if you want the status code and status message of a unsuccessful AP
660660
}
661661
}
662662
```
663+
663664
By default, *failure* FSAs will not contain a `meta` property, while their `payload` property will be evaluated from
664665
```js
665666
(action, state, res) =>
@@ -668,6 +669,9 @@ By default, *failure* FSAs will not contain a `meta` property, while their `payl
668669
)
669670
```
670671
672+
673+
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.
674+
671675
### Exports
672676
673677
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
@@ -1177,7 +1177,7 @@ test('apiMiddleware must dispatch an error request FSA when [RSAA].options fails
11771177
actionHandler(anAction);
11781178
});
11791179

1180-
test('apiMiddleware must dispatch an error request FSA on a request error', t => {
1180+
test('apiMiddleware must dispatch an failure FSA with an error on a request error', t => {
11811181
const anAction = {
11821182
[RSAA]: {
11831183
endpoint: 'http://127.0.0.1/api/users/1', // We haven't mocked this
@@ -1198,7 +1198,9 @@ test('apiMiddleware must dispatch an error request FSA on a request error', t =>
11981198
const doNext = action => {
11991199
switch (action.type) {
12001200
case 'REQUEST':
1201-
if (!action.error) {
1201+
if (action.error) {
1202+
t.fail('Request FSA should not have an error');
1203+
} else {
12021204
t.pass('next handler called');
12031205
t.equal(
12041206
action.type,
@@ -1219,31 +1221,26 @@ test('apiMiddleware must dispatch an error request FSA on a request error', t =>
12191221
action.error,
12201222
'dispatched non-error FSA has correct error property'
12211223
);
1222-
break;
1223-
} else {
1224-
t.pass('next handler called');
1225-
t.equal(
1226-
action.type,
1227-
'REQUEST',
1228-
'dispatched error FSA has correct type property'
1229-
);
1230-
t.equal(
1231-
action.payload.name,
1232-
'RequestError',
1233-
'dispatched error FSA has correct payload property'
1234-
);
1235-
t.equal(
1236-
action.meta,
1237-
'someMeta',
1238-
'dispatched error FSA has correct meta property'
1239-
);
1240-
t.ok(action.error, 'dispatched error FSA has correct error property');
12411224
}
1225+
break;
1226+
case 'FAILURE':
1227+
t.equal(
1228+
action.type,
1229+
'FAILURE',
1230+
'dispatched error FSA has correct type property'
1231+
);
1232+
t.equal(
1233+
action.payload.name,
1234+
'RequestError',
1235+
'dispatched error FSA has correct payload property'
1236+
);
1237+
t.ok(action.error, 'dispatched error FSA has correct error property');
1238+
break;
12421239
}
12431240
};
12441241
const actionHandler = nextHandler(doNext);
12451242

1246-
t.plan(10);
1243+
t.plan(8);
12471244
actionHandler(anAction);
12481245
});
12491246

0 commit comments

Comments
 (0)