Skip to content
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

fix: pass a no-op next function to the last listener middleware #2214

Merged
merged 1 commit into from
Aug 21, 2024
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
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