Skip to content

Commit

Permalink
Merge 96b0691 into 836a3d3
Browse files Browse the repository at this point in the history
  • Loading branch information
nason committed Jun 10, 2018
2 parents 836a3d3 + 96b0691 commit a2f5abb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down Expand Up @@ -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) =>
Expand All @@ -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`.
Expand Down Expand Up @@ -898,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

Expand Down
2 changes: 1 addition & 1 deletion src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ function apiMiddleware({ getState }) {
return next(
await actionWith(
{
...requestType,
...failureType,
payload: new RequestError(e.message),
error: true
},
Expand Down
41 changes: 19 additions & 22 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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);
});

Expand Down

0 comments on commit a2f5abb

Please sign in to comment.