Merge pull request #30 from binotaliu/handle-error-status

add handleStatusCode
This commit is contained in:
Skillz4Killz
2020-05-16 09:18:07 -04:00
committed by GitHub
3 changed files with 32 additions and 4 deletions

View File

@@ -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;

View File

@@ -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.

View File

@@ -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",
}