Skip to content
GitHub

createDiscordError

function in meocord/testing Since 4.1.0

createDiscordError(code: number, message?: string): DiscordAPIError

Creates the error discord.js throws for a failed Discord API call, for a mock to reject with.

Use it to test what a handler or respond() does when Discord refuses a call. Common codes: 10062, the three seconds to answer passed; 40060, already acknowledged; 50001, missing access; 50027, the fifteen-minute token expired.

Parameters

NameTypeDescription
codenumber

The Discord JSON error code.

message?string

The error's message; one naming the code by default.

Returns

DiscordAPIError

Examples

TypeScript
import { expect } from 'vitest'

const interaction = createMockInteraction(ChatInputCommandInteraction, { commandName: 'ping' })
interaction.reply.mockRejectedValueOnce(createDiscordError(10062))

// respond() passes Discord's refusal on to the handler, and a filter or the fallback answers it
await expect(respond(interaction).send('Pong!')).rejects.toMatchObject({ code: 10062 })