From 2d31f2ed1a74ba06a0abfb090f39db2f29768a64 Mon Sep 17 00:00:00 2001 From: Aleksey Levenstein Date: Wed, 2 Aug 2023 17:14:54 +0300 Subject: [PATCH] feat: add context to rate-limited event (#1637) --- docs/_packages/web_api.md | 4 ++-- packages/web-api/src/WebClient.spec.js | 8 ++++---- packages/web-api/src/WebClient.ts | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/_packages/web_api.md b/docs/_packages/web_api.md index 931c4333d..af1988ae7 100644 --- a/docs/_packages/web_api.md +++ b/docs/_packages/web_api.md @@ -633,8 +633,8 @@ const token = process.env.SLACK_TOKEN; const web = new WebClient(token); -web.on(WebClientEvent.RATE_LIMITED, (numSeconds) => { - console.log(`A rate-limiting error occurred and the app is going to retry in ${numSeconds} seconds.`); +web.on(WebClientEvent.RATE_LIMITED, (numSeconds, { url }) => { + console.log(`A rate-limiting error occurred while calling ${url} and the app is going to retry in ${numSeconds} seconds.`); }); ``` diff --git a/packages/web-api/src/WebClient.spec.js b/packages/web-api/src/WebClient.spec.js index b8c80ba15..a6dbd924b 100644 --- a/packages/web-api/src/WebClient.spec.js +++ b/packages/web-api/src/WebClient.spec.js @@ -1143,9 +1143,9 @@ describe('WebClient', function () { .reply(429, {}, { 'retry-after': 0 }); const client = new WebClient(token, { rejectRateLimitedCalls: true }); client.on('rate_limited', spy); - client.apiCall('method') + client.apiCall('method', { foo: 'bar' }) .catch((err) => { - assert(spy.calledOnceWith(0)) + assert(spy.calledOnceWith(0, sinon.match({ url: 'method', body: { foo: 'bar' } }))) scope.done(); done(); }); @@ -1213,9 +1213,9 @@ describe('WebClient', function () { .reply(429, {}, { 'retry-after': 0 }); const client = new WebClient(token, { retryConfig: { retries: 0 } }); client.on('rate_limited', spy); - client.apiCall('method') + client.apiCall('method', { foo: 'bar' }) .catch((err) => { - assert(spy.calledOnceWith(0)) + assert(spy.calledOnceWith(0, sinon.match({ url: 'method', body: { foo: 'bar' } }))) scope.done(); done(); }); diff --git a/packages/web-api/src/WebClient.ts b/packages/web-api/src/WebClient.ts index 0d5cd4b75..d4cacd460 100644 --- a/packages/web-api/src/WebClient.ts +++ b/packages/web-api/src/WebClient.ts @@ -563,7 +563,7 @@ export class WebClient extends Methods { if (response.status === 429) { const retrySec = parseRetryHeaders(response); if (retrySec !== undefined) { - this.emit(WebClientEvent.RATE_LIMITED, retrySec); + this.emit(WebClientEvent.RATE_LIMITED, retrySec, { url, body }); if (this.rejectRateLimitedCalls) { throw new AbortError(rateLimitedErrorWithDelay(retrySec)); }