Skip to content

Commit b9d5eb7

Browse files
andrewsuzukitimche
authored andcommitted
Bypass action and meta creators if payload is an Error object (#66)
* bypass action and meta creators if payload is an Error object
1 parent 2694319 commit b9d5eb7

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/__tests__/createAction-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,18 @@ describe('createAction()', () => {
116116
});
117117
}
118118
});
119+
120+
it('bypasses action creators if payload is an Error object', () => {
121+
const actionCreator = createAction(type, () => 'not this', (_, meta) => meta);
122+
const errObj = new TypeError('this is an error');
123+
124+
const errAction = actionCreator(errObj, { foo: 'bar' });
125+
expect(errAction).to.deep.equal({
126+
type,
127+
payload: errObj,
128+
error: true,
129+
meta: { foo: 'bar' }
130+
});
131+
});
119132
});
120133
});

src/createAction.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@ export default function createAction(type, actionCreator, metaCreator) {
66
const finalActionCreator = typeof actionCreator === 'function'
77
? actionCreator
88
: identity;
9+
910
const actionHandler = (...args) => {
11+
const hasError = args[0] instanceof Error;
12+
1013
const action = {
1114
type
1215
};
1316

14-
const payload = finalActionCreator(...args);
17+
const payload = hasError ? args[0] : finalActionCreator(...args);
1518
if (!(payload === null || payload === undefined)) {
1619
action.payload = payload;
1720
}
1821

19-
if (action.payload instanceof Error) {
22+
if (hasError) {
2023
// Handle FSA errors where the payload is an Error object. Set error.
2124
action.error = true;
2225
}

0 commit comments

Comments
 (0)