Skip to content

Commit

Permalink
fix: pass a no-op next function to the last listener middleware (#2214
Browse files Browse the repository at this point in the history
)
  • Loading branch information
filmaj authored Aug 21, 2024
1 parent d752927 commit 36052ef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/App-built-in-middleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,7 @@ export default class App<AppCustomContext extends StringIndexed = StringIndexed>
// 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) {
Expand All @@ -1156,13 +1156,13 @@ export default class App<AppCustomContext extends StringIndexed = StringIndexed>
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),
);
});
Expand Down

0 comments on commit 36052ef

Please sign in to comment.