message: bette error handling
diff: @@ -12,13 +12,14 @@ import {
  import { logRed } from "../utils/logger.ts";
  import { FetchMembersOptions } from "../types/guild.ts";
  import { delay } from "https://deno.land/std@0.50.0/async/delay.ts";
  +
  let shardSocket: WebSocket;
  /** The session id is needed for RESUME functionality when discord disconnects randomly. */
  let sessionID = "";
  // Discord requests null if no number has yet been sent by discord
  -export let previousSequenceNumber: number | null = null;
  +let previousSequenceNumber: number | null = null;
  let needToResume = false;
  // TODO: If a client does not receive a heartbeat ack between its attempts at sending heartbeats, it should immediately terminate the connection with a non-1000 close code, reconnect, and attempt to resume.
  @@ -43,7 +44,7 @@ async function resumeConnection(
  if (needToResume) resumeConnection(botGatewayData, identifyPayload);
  }
  -export const createShard = async (
  +const createShard = async (
  botGatewayData: DiscordBotGatewayData,
  identifyPayload: object,
  resuming = false,
  @@ -67,7 +68,6 @@ export const createShard = async (
  }));
  }
  - try {
  for await (const message of shardSocket) {
  if (typeof message === "string") {
  const data = JSON.parse(message);
  @@ -108,16 +108,24 @@ export const createShard = async (
  }
  } else if (isWebSocketCloseEvent(message)) {
  logRed(`Close :( ${JSON.stringify(message)}`);
  + // These error codes should just crash the projects
  + if ([4004, 4005, 4012, 4013, 4014].includes(message.code)) {
  + throw new Error(
  + "Shard.ts: Error occurred that is not resumeable or able to be reconnected.",
  + );
  + }
  + // These error codes can not be resumed but need to reconnect from start
  + if ([4003, 4007, 4008, 4009].includes(message.code)) {
  + createShard(botGatewayData, identifyPayload);
  + } else {
  needToResume = true;
  resumeConnection(botGatewayData, identifyPayload);
  }
  }
  - } catch (error) {
  - logRed(error);
  }
  };
  -export function requestGuildMembers(
  +function requestGuildMembers(
  guildID: string,
  nonce: string,
  options?: FetchMembersOptions,
  @@ -135,16 +143,10 @@ export function requestGuildMembers(
  }));
  }
  -// TODO: Remove ts-ignore once https://github.com/denoland/deno/issues/5262 fixed
  -// @ts-ignore
  -if (typeof self.postMessage === "function") {
  - // @ts-ignore
  +// TODO: Errors need to be fixed by VSC plugin
  postMessage({ type: "REQUEST_CLIENT_OPTIONS" });
  -}
  // @ts-ignore
  -if (typeof self.onmessage === "function") {
  - // @ts-ignore
  - onmessage = (message) => {
  +onmessage = (message: MessageEvent) => {
  if (message.data.type === "CREATE_SHARD") {
  createShard(
  message.data.botGatewayData,
  @@ -177,4 +179,3 @@ if (typeof self.onmessage === "function") {
  }));
  }
  };
  -}
  