Repository navigation
feat(plugin-gateway): add discord.js-style error codes - #152
Conversation
◈ PR LensPR Lens could not render |
There was a problem hiding this comment.
✅ 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/: newGatewayError,GatewayTypeError,GatewayRangeError(extendingError/TypeError/RangeErrorrespectively, each carryingcodeand anamesuffixed with it),GatewayErrorCodes(Object.freezed map derived from the messages), andGatewayErrorMessages(one string/function entry per code). Adapted from discord.js'serrorsmodule (Apache-2.0), with attribution headers on each file.- Call-site migration: every
throw new Error/TypeError/RangeError(...)acrossGatewayClient,actions/Action, the managers, structures, andutil/*now throws the matchingGateway*Errorvariant, preserving the original built-in error type. util/errors.ts:DispatchTimeoutError,GuildMembersTimeoutError,GuildMembersRateLimitError, andGatewaySessionStoreErrornow extendGatewayError<Code>instead ofErrordirectly, so theirnamegains the code suffix;GatewaySessionStoreErrorsetscausevia direct assignment post-super()instead ofError'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.tscovers 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.
Claude Sonnet | 𝕏
Adapted from discord.js's errors module (Apache-2.0).
e1d3335 to
85e15c5
Compare
@wolfstar/plugin-api
@wolfstar/plugin-broker
@wolfstar/plugin-cache
@wolfstar/plugin-gateway
@wolfstar/plugin-i18next
@wolfstar/plugin-logger
@wolfstar/plugin-sharder
@wolfstar/plugin-subcommands-advanced
commit: |

🔗 Linked issue
#XXXX
🧭 Context
@wolfstar/plugin-gatewaythrew plainError/TypeError/RangeErrorwith ad-hoc messages, so consumers could only tell errors apart by matching their message. discord.js solves this with itserrorsmodule: every error has a stablecode, 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'serrors(Apache-2.0); each carries an attribution header, and the README gets a Credits section.Existing messages and error types are kept (a
TypeErrorstays aTypeError), so matching oninstanceofor 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
throw new Error(\Webhook ${id} has no token to post with`)`throw new GatewayError("WebhookTokenUnavailable", id)error.code === GatewayErrorCodes.WebhookTokenUnavailablename:Errorname:GatewayError [WebhookTokenUnavailable]DispatchTimeoutError& co. extendErrorGatewayError, so theirnamegains 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+namewith the code, likeDiscordjsError.ThreadMemberwithout its IDs now says "…cannot be removed", in line withfetch.Type of Change
Pre-flight Checklist
pnpm changeset(orpnpm changeset add --emptyif no release is needed). See .changeset/README.md.