Skip to content

Commit

Permalink
feat: add context to rate-limited event (#1637)
Browse files Browse the repository at this point in the history
  • Loading branch information
levenleven authored Aug 2, 2023
1 parent 1c0b30f commit 2d31f2e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions docs/_packages/web_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.`);
});
```
Expand Down
8 changes: 4 additions & 4 deletions packages/web-api/src/WebClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down Expand Up @@ -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();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/web-api/src/WebClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down

0 comments on commit 2d31f2e

Please sign in to comment.