Skip to content

fix(markdown-toolkit): state the rule in errors for wrong input, and throw TypeError - #199

Merged
Jason3S merged 3 commits into
mainfrom
claude/md-assert-condition
Nov 11, 2026
Merged

Jason3S merged 3 commits into
mainfrom
claude/md-assert-condition

Conversation

@Jason3S

@Jason3S Jason3S commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

An error for a wrong argument now states the rule and stops: it no longer says what was given. Seven functions that threw a plain Error for a wrong value now throw a TypeError, so every rejected argument reports the same error class.

  • No message ends in , got … or quotes the value any more.
  • Code that matches the old message text, or checks for Error by name, needs updating.

Messages

Each line is a whole message.

  • heading depth must be a number from 1 to 6
  • a list start must be a whole number from 0 to 999999999
  • column alignment must be 'left', 'center', 'right' or a delimiter such as ':---'
  • a placeholder name must be parts joined by '.', each of ASCII letters, digits, '_' and '-', and not starting with '-'
  • an HTML entity must be a character, a name HTML defines, or a reference such as ' '
  • table header must be an array, and the same for table rows, a table row, list entries and definition list entries
  • heading content must be inline, and the same wherever only text, links, formatting or code can go
  • document content must be blocks, and the same wherever only blocks can go
  • definition list entry must be a [term, definition] pair
  • expected a string or a Markdown element
  • {@ name @} is inside code and takes only a string, text, a placeholder or an HTML entity
  • {@ name @} is inside a URL and takes only a string, text or a placeholder

Error class

These threw Error and now throw TypeError:

  • heading, for a wrong depth
  • table and htmlTable, for a wrong column alignment
  • placeholder and blockPlaceholder, for a wrong name
  • inlineTemplate, for text of more than one paragraph
  • htmlEntity, for an unknown name
Usage
import * as md from '@streetsidesoftware/markdown-toolkit';

try {
  md.heading(7, 'Title');
} catch (error) {
  error instanceof TypeError;
  // true. It was a plain Error before.
  error.message;
  // heading depth must be a number from 1 to 6
}

…rror

Seven checks reject a value that breaks a condition, such as a heading
depth outside 1 to 6. Each had its own `if` and `throw`, and which
error class it threw depended only on when it was written: four threw
`Error`, three threw `TypeError`.

They now call one assert, `assertCondition`, which states the condition
that must hold and always throws a `TypeError`. The messages are
unchanged.

The four that change class are the heading depth, the column alignment,
the placeholder name and the one-paragraph inline template.
An error for a wrong argument now states the rule, type and range, and
stops. It no longer reports what was given: no `, got 7`, no
`, got a string`, and no quoted copy of the value.

Twelve messages change. Nine lose their `, got …` tail, and the heading
one also names the type: "heading depth must be a number from 1 to 6".
Three that only said "invalid" or "unknown" and quoted the value now
state their rule: the column alignment, the placeholder name and the
HTML entity.

With nothing left to describe, `describe` is removed, along with the
line in `list` that chose between a number's value and its kind.
@Jason3S Jason3S changed the title fix(markdown-toolkit): reject wrong values with a TypeError, not an Error fix(markdown-toolkit): state the rule in errors for wrong input, and throw TypeError Nov 11, 2026
@Jason3S
Jason3S marked this pull request as draft November 11, 2026 08:47
Five functions accept their input case by case and reject the rest with
a `throw` on the last line. That line is now `assertUnhandledCase`,
which never returns, so each case line stays as it is and a new case is
still one added line.

`htmlEntity` was the last place to throw a plain `Error`; it now throws
a `TypeError` like every other rejected argument. The check that turns
away a list item inside a line is a condition with its own message, so
it uses `assertCondition`.
@Jason3S
Jason3S marked this pull request as ready for review November 11, 2026 08:51
@Jason3S
Jason3S merged commit 2056b50 into main Nov 11, 2026
8 checks passed
@Jason3S
Jason3S deleted the claude/md-assert-condition branch November 11, 2026 09:02
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