mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-03 01:10:07 +00:00
Merge pull request #30 from binotaliu/handle-error-status
add handleStatusCode
This commit is contained in:
@@ -2,6 +2,7 @@ import { RequestMethod } from "../types/fetch.ts";
|
||||
import { authorization } from "./client.ts";
|
||||
import { delay } from "https://deno.land/std@0.50.0/async/delay.ts";
|
||||
import { Errors } from "../types/errors.ts";
|
||||
import { HttpResponseCode } from "../types/discord.ts";
|
||||
|
||||
const queue: Array<() => Promise<unknown>> = [];
|
||||
const ratelimitedPaths = new Map<string, RateLimitedPath>();
|
||||
@@ -42,6 +43,7 @@ export const RequestManager = {
|
||||
get: async (url: string, body?: unknown) => {
|
||||
await checkRatelimits(url);
|
||||
const result = await fetch(url, createRequestBody(body));
|
||||
handleStatusCode(result.status);
|
||||
processHeaders(url, result.headers);
|
||||
|
||||
return result.json();
|
||||
@@ -99,13 +101,15 @@ async function runMethod(
|
||||
await checkRatelimits(url);
|
||||
const response = await fetch(url, createRequestBody(body, method));
|
||||
processHeaders(url, response.headers);
|
||||
handleStatusCode(response.status);
|
||||
|
||||
// Sometimes Discord returns an empty 204 response that can't be made to JSON.
|
||||
if (response.status === 204) resolve();
|
||||
|
||||
const json = await response.json();
|
||||
if (
|
||||
json.retry_after || json.message === "You are being rate limited."
|
||||
json.retry_after ||
|
||||
json.message === "You are being rate limited."
|
||||
) {
|
||||
if (retryCount > 10) throw new Error(Errors.RATE_LIMIT_RETRY_MAXED);
|
||||
await delay(json.retry_after);
|
||||
@@ -126,6 +130,27 @@ async function runMethod(
|
||||
});
|
||||
}
|
||||
|
||||
function handleStatusCode(status: number): boolean {
|
||||
if (status >= 200 && status < 400) {
|
||||
return true;
|
||||
}
|
||||
|
||||
switch (status) {
|
||||
case HttpResponseCode.BadRequest:
|
||||
case HttpResponseCode.Unauthorized:
|
||||
case HttpResponseCode.Forbidden:
|
||||
case HttpResponseCode.NotFound:
|
||||
case HttpResponseCode.MethodNotAllowed:
|
||||
case HttpResponseCode.TooManyRequests:
|
||||
throw new Error(Errors.REQUEST_CLIENT_ERROR);
|
||||
case HttpResponseCode.GatewayUnavailable:
|
||||
throw new Error(Errors.REQUEST_SERVER_ERROR);
|
||||
}
|
||||
|
||||
// left are all unknown
|
||||
throw new Error(Errors.REQUEST_UNKNOWN_ERROR);
|
||||
}
|
||||
|
||||
function processHeaders(url: string, headers: Headers) {
|
||||
// If a rate limit response is encountered this will become true and returned
|
||||
let ratelimited = false;
|
||||
|
||||
@@ -92,14 +92,14 @@ export enum VoiceCloseEventCode {
|
||||
|
||||
export enum HttpResponseCode {
|
||||
Ok = 200,
|
||||
Created,
|
||||
Created = 201,
|
||||
NoContent = 204,
|
||||
NotModified = 304,
|
||||
BadRequest = 400,
|
||||
Unauthorized = 401,
|
||||
Forbidden = 403,
|
||||
NotFound,
|
||||
MethodNotAllowed,
|
||||
NotFound = 404,
|
||||
MethodNotAllowed = 405,
|
||||
TooManyRequests = 429,
|
||||
GatewayUnavailable = 502,
|
||||
// ServerError left untyped because it's 5xx.
|
||||
|
||||
@@ -23,4 +23,7 @@ export enum Errors {
|
||||
PRUNE_MIN_DAYS = "PRUNE_MIN_DAYS",
|
||||
RATE_LIMIT_RETRY_MAXED = "RATE_LIMIT_RETRY_MAXED",
|
||||
MISSING_INTENT_GUILD_MEMBERS = "MISSING_INTENT_GUILD_MEMBERS",
|
||||
REQUEST_CLIENT_ERROR = "REQUEST_CLIENT_ERROR",
|
||||
REQUEST_SERVER_ERROR = "REQUEST_SERVER_ERROR",
|
||||
REQUEST_UNKNOWN_ERROR = "REQUEST_UNKNOWN_ERROR",
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user