Permalink
Browse files

fix(Reducer errors): log exceptions thrown from `store.dispatch` insi…

…de the middleware (#379)

Closes #263

Most often this is caused by exceptions in your reducers. Right now due to a bug in rxjs these errors can sometimes (but not always) be silently swallowed. This works around it by catching them ourselves and logging and logging them.
  • Loading branch information...
Jerry-Hong authored and jayphelps committed Dec 5, 2017
1 parent e850c93 commit 56c1903ffdc190f33d9c9381a1003ece740cd458
Showing with 32 additions and 2 deletions.
  1. +7 −1 src/createEpicMiddleware.js
  2. +25 −1 test/createEpicMiddleware-spec.js
@@ -53,7 +53,13 @@ export function createEpicMiddleware(rootEpic, options = defaultOptions) {
return output$;
})
::switchMap(output$ => options.adapter.output(output$))
.subscribe(store.dispatch);
.subscribe(action => {
try {
store.dispatch(action);
} catch (err) {
console.error(err);
}
});

// Setup initial root epic
epic$.next(rootEpic);
@@ -70,7 +70,31 @@ describe('createEpicMiddleware', () => {
]);
});

it('should throw if you don\'t provide a rootEpic', () => {
it('should console error when reducer throw exception', () => {
sinon.spy(console, 'error');

const reducer = (state = [], action) => {
switch (action.type) {
case 'ACTION_1':
throw new Error();
default:
return state;
}
};
const epic = (action$, store) =>
mergeStatic(
action$.ofType('FIRE_1')::mapTo({ type: 'ACTION_1' }),
action$.ofType('FIRE_2')::mapTo({ type: 'ACTION_2' })
);
const middleware = createEpicMiddleware(epic);
const store = createStore(reducer, applyMiddleware(middleware));

store.dispatch({ type: 'FIRE_1' });
expect(console.error.callCount).to.equal(1);
console.error.restore();
});

it("should throw if you don't provide a rootEpic", () => {
expect(() => {
createEpicMiddleware();
}).to.throw('You must provide a root Epic to createEpicMiddleware');

0 comments on commit 56c1903

Please sign in to comment.