Skip to content

Commit

Permalink
misc: update too many requests errors (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
radulucut authored Nov 18, 2024
1 parent a89d3d4 commit 2597134
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 19 deletions.
91 changes: 75 additions & 16 deletions packages/slack/src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,54 @@ export const postAPI = async (
try {
measurements = await postMeasurement(postMeasurements, token?.access_token);
} catch (error) {
let e = error;

if (
error instanceof PostError
&& (error.response.statusCode === 401
|| error.response.statusCode === 403)
&& token
) {
const errMsg = await oauth.TryToRefreshToken(
payload.installationId,
token,
);

if (errMsg) {
e = new Error(errMsg);
if (error instanceof PostError) {
const { statusCode, headers } = error.response;

// Unauthorized or Forbidden
if ((statusCode === 401 || statusCode === 403) && token) {
const errMsg = await oauth.TryToRefreshToken(
payload.installationId,
token,
);

if (errMsg) {
throw new Error(errMsg);
}
}

// Too many requests
if (statusCode === 429) {
const rateLimitRemaining = parseInt(headers['X-RateLimit-Remaining'] as string);
const rateLimitReset = parseInt(headers['X-RateLimit-Reset'] as string);
const creditsRemaining = parseInt(headers['X-Credits-Remaining'] as string);
const requestCost = parseInt(headers['X-Request-Cost'] as string);
const remaining = rateLimitRemaining + creditsRemaining;

if (token && !token.isAnonymous) {
if (remaining > 0) {
throw getMoreCreditsRequiredAuthError(
requestCost,
remaining,
rateLimitReset,
);
}

throw getNoCreditsAuthError(rateLimitReset);
} else {
if (remaining > 0) {
throw getMoreCreditsRequiredNoAuthError(
remaining,
requestCost,
rateLimitReset,
);
}

throw getNoCreditsNoAuthError(rateLimitReset);
}
}
}

throw e;
throw error;
}

await client.chat.postEphemeral({
Expand Down Expand Up @@ -329,3 +358,33 @@ Credits:

return text;
}

export function getMoreCreditsRequiredAuthError (
requestCost: number,
remaining: number,
rateLimitReset: number,
): Error {
return new Error(`You only have ${pluralize(
remaining,
'credit',
)} remaining, and ${requestCost} were required. Try requesting fewer probes or wait ${formatSeconds(rateLimitReset)} for the rate limit to reset. You can get higher limits by sponsoring us or hosting probes.`);
}

export function getNoCreditsAuthError (rateLimitReset: number): Error {
return new Error(`You have run out of credits for this session. You can wait ${formatSeconds(rateLimitReset)} for the rate limit to reset or get higher limits by sponsoring us or hosting probes.`);
}

export function getMoreCreditsRequiredNoAuthError (
requestCost: number,
remaining: number,
rateLimitReset: number,
): Error {
return new Error(`You only have ${pluralize(
remaining,
'credit',
)} remaining, and ${requestCost} were required. Try requesting fewer probes or wait ${formatSeconds(rateLimitReset)} for the rate limit to reset. You can get higher limits by creating an account. Sign up at https://globalping.io`);
}

export function getNoCreditsNoAuthError (rateLimitReset: number): Error {
return new Error(`You have run out of credits for this session. You can wait ${formatSeconds(rateLimitReset)} for the rate limit to reset or get higher limits by creating an account. Sign up at https://globalping.io`);
}
40 changes: 37 additions & 3 deletions packages/slack/src/tests/post.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { getLimitsOutput as getLimitsText } from '../post.js';
import {
getLimitsOutput,
getMoreCreditsRequiredAuthError,
getMoreCreditsRequiredNoAuthError,
getNoCreditsAuthError,
getNoCreditsNoAuthError,
} from '../post.js';
import {
CreateLimitType,
IntrospectionResponse,
Expand Down Expand Up @@ -44,7 +50,7 @@ Credits:
- 1000 credits remaining (may be used to create measurements above the hourly limits)
`;

expect(getLimitsText(limits, introspection)).toBe(expectedText);
expect(getLimitsOutput(limits, introspection)).toBe(expectedText);
});

it('should return correct text - limit type IP', () => {
Expand Down Expand Up @@ -72,7 +78,35 @@ Creating measurements:
- 0 consumed, 500 remaining
`;

expect(getLimitsText(limits, introspection)).toBe(expectedText);
expect(getLimitsOutput(limits, introspection)).toBe(expectedText);
});
});

describe('getMoreCreditsRequiredAuthError', () => {
it('should return correct error', () => {
const error = getMoreCreditsRequiredAuthError(5, 2, 300);
expect(error).toEqual(new Error('You only have 2 credits remaining, and 5 were required. Try requesting fewer probes or wait 5 minutes for the rate limit to reset. You can get higher limits by sponsoring us or hosting probes.'));
});
});

describe('getNoCreditsAuthError', () => {
it('should return correct error', () => {
const error = getNoCreditsAuthError(60);
expect(error).toEqual(new Error('You have run out of credits for this session. You can wait 1 minute for the rate limit to reset or get higher limits by sponsoring us or hosting probes.'));
});
});

describe('getMoreCreditsRequiredNoAuthError', () => {
it('should return correct error', () => {
const error = getMoreCreditsRequiredNoAuthError(2, 1, 245);
expect(error).toEqual(new Error('You only have 1 credit remaining, and 2 were required. Try requesting fewer probes or wait 4 minutes for the rate limit to reset. You can get higher limits by creating an account. Sign up at https://globalping.io'));
});
});

describe('getNoCreditsNoAuthError', () => {
it('should return correct error', () => {
const error = getNoCreditsNoAuthError(10);
expect(error).toEqual(new Error('You have run out of credits for this session. You can wait 10 seconds for the rate limit to reset or get higher limits by creating an account. Sign up at https://globalping.io'));
});
});
});

0 comments on commit 2597134

Please sign in to comment.