Skip to content

Commit fda1dc5

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 af94473 commit fda1dc5

File tree

3 files changed

+25
-25
lines changed

3 files changed

+25
-25
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ The `[RSAA].types` property controls the output of `redux-api-middleware`. The s
224224
- `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));
225225
- a network failure occurs (the network is unreachable, the server responds with an error,...).
226226

227-
If such an error occurs, a different *request* FSA will be dispatched (*instead* of the one described above). It will contain the following properties:
228-
- `type`: the string constant in the first position of the `[RSAA].types` array;
227+
If such an error occurs, a *failure* FSA will be dispatched containing the following properties:
228+
- `type`: the string constant in the last position of the `[RSAA].types` array;
229229
- `payload`: a [`RequestError`](#requesterror) object containing an error message;
230230
- `error: true`.
231231

@@ -410,6 +410,7 @@ For example, if you want the status code and status message of a unsuccessful AP
410410
}
411411
}
412412
```
413+
413414
By default, *failure* FSAs will not contain a `meta` property, while their `payload` property will be evaluated from
414415
```js
415416
(action, state, res) =>
@@ -418,6 +419,8 @@ By default, *failure* FSAs will not contain a `meta` property, while their `payl
418419
)
419420
```
420421
422+
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.
423+
421424
## Reference
422425
423426
### Exports

src/middleware.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ function apiMiddleware({ getState }) {
160160
return next(
161161
await actionWith(
162162
{
163-
...requestType,
163+
...failureType,
164164
payload: new RequestError(e.message),
165165
error: true
166166
},

test/index.js

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

1154-
test('apiMiddleware must dispatch an error request FSA on a request error', t => {
1154+
test('apiMiddleware must dispatch an failure FSA with an error on a request error', t => {
11551155
const anAction = {
11561156
[RSAA]: {
11571157
endpoint: 'http://127.0.0.1/api/users/1', // We haven't mocked this
@@ -1172,7 +1172,9 @@ test('apiMiddleware must dispatch an error request FSA on a request error', t =>
11721172
const doNext = action => {
11731173
switch (action.type) {
11741174
case 'REQUEST':
1175-
if (!action.error) {
1175+
if (action.error) {
1176+
t.fail('Request FSA should not have an error');
1177+
} else {
11761178
t.pass('next handler called');
11771179
t.equal(
11781180
action.type,
@@ -1193,31 +1195,26 @@ test('apiMiddleware must dispatch an error request FSA on a request error', t =>
11931195
action.error,
11941196
'dispatched non-error FSA has correct error property'
11951197
);
1196-
break;
1197-
} else {
1198-
t.pass('next handler called');
1199-
t.equal(
1200-
action.type,
1201-
'REQUEST',
1202-
'dispatched error FSA has correct type property'
1203-
);
1204-
t.equal(
1205-
action.payload.name,
1206-
'RequestError',
1207-
'dispatched error FSA has correct payload property'
1208-
);
1209-
t.equal(
1210-
action.meta,
1211-
'someMeta',
1212-
'dispatched error FSA has correct meta property'
1213-
);
1214-
t.ok(action.error, 'dispatched error FSA has correct error property');
12151198
}
1199+
break;
1200+
case 'FAILURE':
1201+
t.equal(
1202+
action.type,
1203+
'FAILURE',
1204+
'dispatched error FSA has correct type property'
1205+
);
1206+
t.equal(
1207+
action.payload.name,
1208+
'RequestError',
1209+
'dispatched error FSA has correct payload property'
1210+
);
1211+
t.ok(action.error, 'dispatched error FSA has correct error property');
1212+
break;
12161213
}
12171214
};
12181215
const actionHandler = nextHandler(doNext);
12191216

1220-
t.plan(10);
1217+
t.plan(8);
12211218
actionHandler(anAction);
12221219
});
12231220

0 commit comments

Comments
 (0)