mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 09:03:29 +01:00
refactor(proxy): rely on auth header instead (#9422)
* refactor(proxy): rely on auth header instead * chore: typo * chore: language Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> * chore: more language Co-authored-by: Aura Román <kyradiscord@gmail.com> * chore: more language nitpicks Co-authored-by: ckohen <chaikohen@gmail.com> * fix: unnecessary async --------- Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> Co-authored-by: Aura Román <kyradiscord@gmail.com> Co-authored-by: ckohen <chaikohen@gmail.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import type { ServerResponse } from 'node:http';
|
||||
import { pipeline } from 'node:stream/promises';
|
||||
import type { DiscordAPIError, HTTPError, RateLimitError } from '@discordjs/rest';
|
||||
import { DiscordAPIError, HTTPError, RateLimitError } from '@discordjs/rest';
|
||||
import type { Dispatcher } from 'undici';
|
||||
|
||||
/**
|
||||
@@ -59,3 +59,24 @@ export function populateAbortErrorResponse(res: ServerResponse): void {
|
||||
res.statusCode = 504;
|
||||
res.statusMessage = 'Upstream timed out';
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to populate a server response from an error object
|
||||
*
|
||||
* @param res - The server response to populate
|
||||
* @param error - The error to check and use
|
||||
* @returns - True if the error is known and the response object was populated, otherwise false
|
||||
*/
|
||||
export function populateErrorResponse(res: ServerResponse, error: unknown): boolean {
|
||||
if (error instanceof DiscordAPIError || error instanceof HTTPError) {
|
||||
populateGeneralErrorResponse(res, error);
|
||||
} else if (error instanceof RateLimitError) {
|
||||
populateRatelimitErrorResponse(res, error);
|
||||
} else if (error instanceof Error && error.name === 'AbortError') {
|
||||
populateAbortErrorResponse(res);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user