Skip to content

Commit 3e6509d

Browse files
committed
Handle errors thrown by listeners
1 parent 2f2c4d2 commit 3e6509d

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

packages/action-listener-middleware/src/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,15 @@ export function createActionListenerMiddleware<
249249
continue
250250
}
251251

252-
entry.listener(action, {
253-
...api,
254-
currentPhase,
255-
unsubscribe: entry.unsubscribe,
256-
})
252+
try {
253+
entry.listener(action, {
254+
...api,
255+
currentPhase,
256+
unsubscribe: entry.unsubscribe,
257+
})
258+
} catch (err) {
259+
// ignore
260+
}
257261
}
258262
if (currentPhase === 'beforeReducer') {
259263
result = next(action)

packages/action-listener-middleware/src/tests/listenerMiddleware.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,18 @@ describe('createActionListenerMiddleware', () => {
298298

299299
expect(reducer.mock.calls).toEqual([[{}, testAction1('a')]])
300300
})
301+
302+
test('Continues running other listeners if there is an error', () => {
303+
const matcher = (action: any) => true
304+
305+
middleware.addListener(matcher, () => {
306+
throw new Error('Panic!')
307+
})
308+
309+
const listener = jest.fn(() => {})
310+
middleware.addListener(matcher, listener)
311+
312+
store.dispatch(testAction1('a'))
313+
expect(listener.mock.calls).toEqual([[testAction1('a'), middlewareApi]])
314+
})
301315
})

0 commit comments

Comments
 (0)