feat(client): add runtime isError guards to all public ...Error unions#7
Merged
Conversation
Each exported `...Error` union now has a same-named runtime constant
with an `isError(error: unknown): error is ...Error` type guard built
from `instanceof` checks, enabling exhaustive switch-on-name patterns:
if (ListMarketsError.isError(error)) {
switch (error.name) { ... }
}
- Add `makeErrorGuard(...classes)` helper in `errors.ts`; infers the
guard union type from the constructor tuple — single source of truth
- Add `export const FooError = makeErrorGuard(A, B, C)` alongside
every `export type FooError` across 27 source files (actions, abis,
types, workflow)
- Re-export `./actions` from the root entry point so guards are
accessible from `@polymarket/client` without a sub-path import
- Add `errors.test.ts` covering positive hits, non-SDK rejection, and
type-narrowing behaviour
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ctions Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
makeErrorGuard(...classes)helper inerrors.tsthat takes concrete error constructors and returns{ isError(error: unknown): error is Union }viainstanceofchecks — the union type is inferred from the constructor tupleexport const FooError = makeErrorGuard(...)alongside everyexport type FooErrorunion across 27 source files (all action files,abis.ts,types.ts,workflow.ts)./actionsfrom the rootindex.tsso guards are accessible from@polymarket/clientwithout a sub-path importerrors.test.tswith 3 tests covering positive hits, non-SDK rejection, and type-narrowingConsumer ergonomics
Both the existing
import type { ListMarketsError }(union type) and the newimport { ListMarketsError }(guard object) are exported under the same name, using TypeScript's separate type/value namespaces — no breaking change.Design notes
instanceof, noterror.namestring matching, so subclasses are handled correctlyPrepareMarketOrderPostingError = PrepareMarketOrderError), the const is a direct reference to the parent guard — no duplicationgasless.tswhere the inner type is module-private (ExecuteGaslessError), the classes are flattened at the call siteWaitForTransactionErrorintypes.tsimports classes directly from./errorsto avoid a circular value dependency through./actionsTest plan
pnpm lint— cleanpnpm typecheck— cleanpnpm vitest run --project client packages/client/src/errors.test.ts— 3/3 pass🤖 Generated with Claude Code
Note
Low Risk
Low risk: changes are additive (new
makeErrorGuardhelper and exported guard values) and don’t alter request/transaction behavior, but they do expand the public API surface via additional exports.Overview
Adds a generic
makeErrorGuard(...classes)helper and, alongside each existing...Errortype union, exports a same-namedconstwith anisError(error)runtime type guard inferred from the provided error constructors.Updates decorators to re-export these error guards/unions per action group, adjusts root exports (including
AuthenticateWithError/CompleteWithError) so consumers can import guards from the package entrypoint, and adds a smallvitestsuite validating guard behavior and TypeScript narrowing.Reviewed by Cursor Bugbot for commit 6af2b1f. Bugbot is set up for automated code reviews on this repo. Configure here.