Skip to content

Commit

Permalink
fix(proxyRequests): handle 429 accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
didinele committed May 18, 2022
1 parent db007b1 commit aaaa353
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
16 changes: 11 additions & 5 deletions packages/proxy/src/handlers/proxyRequests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { URL } from 'node:url';
import { DiscordAPIError, RequestMethod, REST, RouteLike } from '@discordjs/rest';
import { populateErrorResponse, populateOkResponse } from '../util/responseHelpers';
import { DiscordAPIError, RateLimitError, RequestMethod, REST, RouteLike } from '@discordjs/rest';
import {
populateGeneralErrorResponse,
populateOkResponse,
populateRatelimitErrorResponse,
} from '../util/responseHelpers';
import type { RequestHandler } from '../util/util';

/**
Expand Down Expand Up @@ -32,13 +36,15 @@ export function proxyRequests(rest: REST): RequestHandler {

await populateOkResponse(res, discordResponse);
} catch (error) {
if (!(error instanceof DiscordAPIError)) {
if (error instanceof DiscordAPIError) {
populateGeneralErrorResponse(res, error);
} else if (error instanceof RateLimitError) {
populateRatelimitErrorResponse(res, error);
} else {
// Unclear if there's better course of action here. Any web framework allows to pass in an error handler for something like this
// at which point the user could dictate what to do with the error - otherwise we could just 500
throw error;
}

populateErrorResponse(res, error);
} finally {
res.end();
}
Expand Down
16 changes: 13 additions & 3 deletions packages/proxy/src/util/responseHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ServerResponse } from 'node:http';
import { DiscordAPIError, parseResponse } from '@discordjs/rest';
import { DiscordAPIError, parseResponse, RateLimitError } from '@discordjs/rest';
import type { Dispatcher } from 'undici';

/**
Expand All @@ -24,13 +24,23 @@ export async function populateOkResponse(res: ServerResponse, data: Dispatcher.R
}

/**
* Populates a server response with the data from a Discord non-2xx REST response
* Populates a server response with the data from a Discord non-2xx REST response that is NOT a 429
* @param res The server response to populate
* @param error The error to populate the response with
*/
export function populateErrorResponse(res: ServerResponse, error: DiscordAPIError): void {
export function populateGeneralErrorResponse(res: ServerResponse, error: DiscordAPIError): void {
res.statusCode = error.status;
res.statusMessage = error.message;
res.setHeader('Content-Type', 'application/json');
res.write(JSON.stringify(error.rawError));
}

/**
* Populates a server response with the data from a Discord 429 REST response
* @param res The server response to populate
* @param error The error to populate the response with
*/
export function populateRatelimitErrorResponse(res: ServerResponse, error: RateLimitError): void {
res.statusCode = 429;
res.setHeader('Retry-After', error.timeToReset / 1000);
}

0 comments on commit aaaa353

Please sign in to comment.