Skip to content

Always log reducers exception #263 #379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/createEpicMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 25 additions & 1 deletion test/createEpicMiddleware-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down