Skip to content

Commit 6fd9b05

Browse files
committed
Catch and rethrow error from reducer init in combineReducers
1 parent 96e8b3e commit 6fd9b05

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/utils/combineReducers.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,15 @@ export default function combineReducers(reducers) {
7676

7777
Object.keys(finalReducers).forEach(key => {
7878
var reducer = finalReducers[key];
79-
if (!sanityError && typeof reducer(undefined, { type: ActionTypes.INIT }) === 'undefined') {
79+
var initialState;
80+
81+
try {
82+
initialState = reducer(undefined, { type: ActionTypes.INIT });
83+
} catch (e) {
84+
sanityError = e;
85+
}
86+
87+
if (!sanityError && typeof initialState === 'undefined') {
8088
sanityError = new Error(
8189
`Reducer "${key}" returned undefined during initialization. ` +
8290
`If the state passed to the reducer is undefined, you must ` +

test/utils/combineReducers.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ describe('Utils', () => {
8484
);
8585
});
8686

87+
it('should catch error thrown in reducer when initializing and re-throw', () => {
88+
const reducer = combineReducers({
89+
throwingReducer() {
90+
throw new Error('Error thrown in reducer');
91+
}
92+
});
93+
expect(() => reducer({})).toThrow(
94+
/Error thrown in reducer/
95+
);
96+
});
97+
8798
it('should allow a symbol to be used as an action type', () => {
8899
const increment = Symbol('INCREMENT');
89100

0 commit comments

Comments
 (0)