From 8a298d0b6d1c939996445a9d6dbd1f8d7c282b85 Mon Sep 17 00:00:00 2001 From: BinotaLIU Date: Sat, 16 May 2020 21:08:25 +0800 Subject: [PATCH] add handleStatusCode --- module/requestManager.ts | 27 ++++++++++++++++++++++++++- types/discord.ts | 6 +++--- types/errors.ts | 3 +++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/module/requestManager.ts b/module/requestManager.ts index 48ac5026d..12e7f1f51 100644 --- a/module/requestManager.ts +++ b/module/requestManager.ts @@ -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> = []; const ratelimitedPaths = new Map(); @@ -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; diff --git a/types/discord.ts b/types/discord.ts index 5777ee238..f927877ea 100644 --- a/types/discord.ts +++ b/types/discord.ts @@ -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. diff --git a/types/errors.ts b/types/errors.ts index 384393480..c91cc1fad 100644 --- a/types/errors.ts +++ b/types/errors.ts @@ -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", }