diff --git a/src/App-built-in-middleware.spec.ts b/src/App-built-in-middleware.spec.ts index 0f85f19c9..1d118a0f4 100644 --- a/src/App-built-in-middleware.spec.ts +++ b/src/App-built-in-middleware.spec.ts @@ -366,6 +366,19 @@ describe('App built-in middleware and mechanism', () => { assert.throws(() => app.event('message.channels', async () => {}), 'Although the document mentions'); assert.throws(() => app.event(/message\..+/, async () => {}), 'Although the document mentions'); }); + + // https://github.com/slackapi/bolt-js/issues/1457 + it('should not cause a runtime exception if the last listener middleware invokes next()', async () => new Promise((resolve, reject) => { + app.event('app_mention', async ({ next }) => { + try { + await next(); + resolve(); + } catch (e) { + reject(e); + } + }); + fakeReceiver.sendEvent(createDummyReceiverEvent('app_mention')); + })); }); describe('middleware and listener arguments', () => { diff --git a/src/App.ts b/src/App.ts index 184063f81..2f9f98d7d 100644 --- a/src/App.ts +++ b/src/App.ts @@ -1143,7 +1143,7 @@ export default class App // Copy the array so modifications don't affect the original const listenerMiddleware = [...origListenerMiddleware]; - // Don't process the last item in the listenerMiddleware array - it shouldn't get a next fn + // Don't process the last item in the listenerMiddleware array - it will be passed a no-op next fn const listener = listenerMiddleware.pop(); if (listener === undefined) { @@ -1156,13 +1156,13 @@ export default class App client, this.logger, // When all the listener middleware are done processing, - // `listener` here will be called without a `next` execution + // `listener` here will be called with a noop `next` fn async () => listener({ ...(listenerArgs as AnyMiddlewareArgs), context, client, logger: this.logger, - // `next` is already set in the outer processMiddleware + next: () => {}, } as AnyMiddlewareArgs & AllMiddlewareArgs), ); });