Skip to content

feat: typed, descriptive errors for mapping misuse and cardinality violations#89

Draft
jeswr wants to merge 1 commit into
rdfjs:mainfrom
jeswr:feat/typed-mapping-errors
Draft

feat: typed, descriptive errors for mapping misuse and cardinality violations#89
jeswr wants to merge 1 commit into
rdfjs:mainfrom
jeswr:feat/typed-mapping-errors

Conversation

@jeswr

@jeswr jeswr commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

What this does

Adds typed, descriptive errors for mapping misuse and cardinality violations, replacing the bare throw new Error // TODO: Describe statements and the two untyped string Errors in RequiredFrom (resolving its // TODO: Expose standard errors). Consumers can now catch and discriminate these failures programmatically instead of matching on message strings.

Two new error classes, exported from mod.ts:

  • MappingArgumentError (extends WrapperError) — thrown when a mapping function is invoked with an undefined termAs/termFrom mapper (e.g. a misspelt LiteralAs member in plain JavaScript). Carries the offending parameter name in argument. Now thrown by RequiredFrom.subjectPredicate, OptionalFrom.subjectPredicate, OptionalAs.object, SetFrom.subjectPredicate and Mapping.languageDictionary.
  • CardinalityError (extends TermError, so rooted at WrapperError) — thrown by RequiredFrom.subjectPredicate when a singular mapping finds no value or more than one value. Carries the subject via TermError.term, the predicate IRI, and a found: "none" | "multiple" discriminator. The existing message texts are preserved verbatim, so this is not a breaking change for anyone matching on messages.

Both classes carry the repo-style dense JSDoc (summary, @remarks, titled @example blocks pairing Turtle with a mapping class, @see), and the five throwing mapping functions gained @param/@returns/@throws documentation. npx typedoc passes with no new warnings.

Tests

  • Upgrades the existing assert.throws(() => ...) assertions in test/unit/term_wrapper.test.ts to assert the specific CardinalityError class and its found/predicate/term properties (removing its // TODO: Test for specific errors and // TODO: check for typed error singular no value comments).
  • New test/unit/mapping_errors.test.ts covering MappingArgumentError (including the argument name) from all five entry points. Both new files are at 100% line/branch coverage; full suite passes (129 tests, 0 fail).

Issues

Relation to #61 / #73 / #74

Supersedes no part of them: all three leave these TODO-marked bare throws untouched (#61 keeps the throw new Error // TODO: Describe lines as-is in its rewrite of the same functions). Merge-order conflicts with #61 in these five files would be textual and small.

Review timing: This draft was prepared with Claude; I (@jeswr) will personally review it before it progresses. I'm currently batching a lot of work in flight, so expect active review Wednesday-Friday (8-10 July).

…olations

Adds two error classes rooted at WrapperError and exports them from mod.ts:

- MappingArgumentError: thrown when a mapping function is invoked with an
  undefined termAs/termFrom mapper, carrying the offending argument name.
  Replaces the bare 'throw new Error // TODO: Describe' in RequiredFrom,
  OptionalFrom, OptionalAs, SetFrom and Mapping.
- CardinalityError: thrown by RequiredFrom when a singular mapping finds
  no value or more than one value, carrying the subject term, the
  predicate IRI and a 'none' | 'multiple' discriminator. Replaces the two
  untyped string Errors in RequiredFrom (messages unchanged) and resolves
  its 'TODO: Expose standard errors'.

Documents the new errors with @throws tags on the throwing mapping
functions and upgrades the existing assert.throws tests to assert the
specific error classes and properties.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant