Skip to content

feat(plugin-gateway): add discord.js-style error codes - #152

Merged
kodiakhq[bot] merged 1 commit into
mainfrom
feat/plugin-gateway-error-codes
Sep 28, 2026
Merged

kodiakhq[bot] merged 1 commit into
mainfrom
feat/plugin-gateway-error-codes

Conversation

@RedStar071

Copy link
Copy Markdown
Member

🔗 Linked issue

#XXXX

🧭 Context

@wolfstar/plugin-gateway threw plain Error/TypeError/RangeError with ad-hoc messages, so consumers could only tell errors apart by matching their message. discord.js solves this with its errors module: every error has a stable code, and messages live in a single table.

📚 Description

This ports that module to the gateway plugin, under src/errors/, and moves every error the package throws or emits onto it. The files are adapted from discord.js's errors (Apache-2.0); each carries an attribution header, and the README gets a Credits section.

Existing messages and error types are kept (a TypeError stays a TypeError), so matching on instanceof or on the message keeps working. Where a case matches one of discord.js's codes, its name is reused (MessageContentType, MemberFetchNonceLength, WebhookTokenUnavailable, VoiceStateNotOwn, NotGuildSticker, GuildChannelOrphan, …).

Key changes

Before After
throw new Error(\Webhook ${id} has no token to post with`)` throw new GatewayError("WebhookTokenUnavailable", id)
No way to identify an error but its message error.code === GatewayErrorCodes.WebhookTokenUnavailable
name: Error name: GatewayError [WebhookTokenUnavailable]
DispatchTimeoutError & co. extend Error They extend GatewayError, so their name gains the code (GuildMembersTimeoutError [GuildMembersTimeout])
  • GatewayErrorMessages: one entry per code, a string or a function of typed arguments (new GatewayError("ChannelGuildUnknown") without the ID does not compile).
  • GatewayErrorCodes: frozen map of every code to itself, derived from the messages.
  • GatewayError, GatewayTypeError, GatewayRangeError: code + name with the code, like DiscordjsError.
  • One message changed slightly: removing a ThreadMember without its IDs now says "…cannot be removed", in line with fetch.

Type of Change

  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation change
  • Workflow / CI change

Pre-flight Checklist

  • I have read the Contributing Guidelines (if applicable)
  • My code follows the code style of this project
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the documentation accordingly
  • I added a changeset with pnpm changeset (or pnpm changeset add --empty if no release is needed). See .changeset/README.md.

@RedStar071 RedStar071 added the packages:plugin-gateway Changes in plugin-gateway label Sep 28, 2026
@coldtea-pr-lens

coldtea-pr-lens Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

◈ PR Lens

PR Lens could not render e1d3335: the analysis run did not complete. Push a new commit to try again.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

This PR ports discord.js's errors module into @wolfstar/plugin-gateway, so every error the package throws or emits carries a stable code and a message pulled from a single table.

  • src/errors/: new GatewayError, GatewayTypeError, GatewayRangeError (extending Error/TypeError/RangeError respectively, each carrying code and a name suffixed with it), GatewayErrorCodes (Object.freezed map derived from the messages), and GatewayErrorMessages (one string/function entry per code). Adapted from discord.js's errors module (Apache-2.0), with attribution headers on each file.
  • Call-site migration: every throw new Error/TypeError/RangeError(...) across GatewayClient, actions/Action, the managers, structures, and util/* now throws the matching Gateway*Error variant, preserving the original built-in error type.
  • util/errors.ts: DispatchTimeoutError, GuildMembersTimeoutError, GuildMembersRateLimitError, and GatewaySessionStoreError now extend GatewayError<Code> instead of Error directly, so their name gains the code suffix; GatewaySessionStoreError sets cause via direct assignment post-super() instead of Error's options bag, functionally equivalent and covered by the new test.
  • Docs & tests: README gains an "Errors" section and a "Credits" section; tests/errors.test.ts covers code/name/message formatting, the built-in-type variants, and subclass retention.

I cross-checked every converted throw site by hand against its original error type and against the GatewayErrorMessages entry's parameter order — all ~35 sites (managers, structures, mixins, utils) preserve the original Error/TypeError/RangeError choice and pass arguments in the order the message function expects. No mismatches found. The one intentional message-text change (ThreadMember#remove, now worded like fetch) is called out in the PR description.

ℹ️ .name/message text changed under a minor release

Every thrown error's name now carries its code (e.g. plain TypeError → GatewayTypeError [ChannelLockPermissionsConflict]), and ThreadMember#remove's message text changed. Neither was previously documented API, so shipping this as minor is defensible, but it's worth the maintainers explicitly confirming: a consumer matching on error.name or an exact error.message string (rather than instanceof + error.code, which this PR is designed to make the supported pattern) would observe different values after upgrading.

Pullfrog  | View workflow run | Using Claude Sonnet | 𝕏

Adapted from discord.js's errors module (Apache-2.0).
@RedStar071
RedStar071 force-pushed the feat/plugin-gateway-error-codes branch from e1d3335 to 85e15c5 Compare September 28, 2026 13:48
@pkg-pr-new

pkg-pr-new Bot commented Sep 28, 2026

Copy link
Copy Markdown

Open in StackBlitz

@wolfstar/plugin-api

pnpm add https://pkg.pr.new/@wolfstar/plugin-api@152

@wolfstar/plugin-broker

pnpm add https://pkg.pr.new/@wolfstar/plugin-broker@152

@wolfstar/plugin-cache

pnpm add https://pkg.pr.new/@wolfstar/plugin-cache@152

@wolfstar/plugin-gateway

pnpm add https://pkg.pr.new/@wolfstar/plugin-gateway@152

@wolfstar/plugin-i18next

pnpm add https://pkg.pr.new/@wolfstar/plugin-i18next@152

@wolfstar/plugin-logger

pnpm add https://pkg.pr.new/@wolfstar/plugin-logger@152

@wolfstar/plugin-sharder

pnpm add https://pkg.pr.new/@wolfstar/plugin-sharder@152

@wolfstar/plugin-subcommands-advanced

pnpm add https://pkg.pr.new/@wolfstar/plugin-subcommands-advanced@152

commit: 85e15c5

@kodiakhq
kodiakhq Bot merged commit d853792 into main Sep 28, 2026
15 of 16 checks passed
@kodiakhq
kodiakhq Bot deleted the feat/plugin-gateway-error-codes branch September 28, 2026 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

packages:plugin-gateway Changes in plugin-gateway

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant