discord-error
TypeScript icon, indicating that this package has built-in type declarations

3.0.1 • Public • Published

discord-error

Creates a JavaScript Error from the body response of a Discord API JSON error message. It is intended to be used primarily in libraries rather than client code.

Usage

import { DiscordError, isDiscordError } from "discord-error";

// This should be the incoming interaction request
const request = new Request();

// A request to the Discord API that performs some action
const response = await fetch(DISCORD_API, { ...options });

if (!response.ok) {
	const data = await response.json();
	const label = isDiscordError(data) ? data.code : data.error;
	throw new DiscordError(request, response, label, data);
}

Credits

This code is derived from code originally written by the discord.js maintainers in the @discordjs/rest package and distributed under the Apache 2 license. It was changed to be built with Web APIs instead of Node.js APIs.

Readme

Keywords

none

Package Sidebar

Install

npm i discord-error

Weekly Downloads

58

Version

3.0.1

License

Apache-2.0

Unpacked Size

26.1 kB

Total Files

11

Last publish

Collaborators

  • ianmitchell