Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

generate-error-docs.mjs

97 lines3.3 KB

1// Writes docs/errors.md, the page production error messages link to. Each
2// coded error class gets an `error-<code>` anchor and its development messages
3// for the sample inputs in packages/db/tests/error-sample-arguments.ts. Each
4// coded error site, a plain Error thrown elsewhere, gets an anchor, its source
5// file, and its message template.
6// With `--check`, it fails instead of writing when the page is out of date.
7// With `--stdout`, it emits the fresh page without reading or writing docs.
8// packages/db/tests/production-error-messages-oracle.test.ts checks the headings.
9import { readFile, writeFile } from 'node:fs/promises'
10import path from 'node:path'
11import { fileURLToPath } from 'node:url'
12​
13const args = process.argv.slice(2)
14const [mode] = args
15if (
16 args.length > 1 ||
17 (mode !== undefined && !['--check', '--stdout'].includes(mode))
18) {
19 console.error(
20 'Usage: node scripts/generate-error-docs.mjs [--check | --stdout]',
21 )
22 process.exit(1)
24​
25const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
26const fixtures = path.join(root, 'packages/db/tests/fixtures')
27const read = async (name) =>
28 JSON.parse(await readFile(path.join(fixtures, name), 'utf8'))
29const codes = await read('error-codes.json')
30const messages = await read('error-messages.json')
31const sites = await read('error-site-messages.json')
32​
33const sections = Object.entries(codes).map(
34 ([name, code]) => `<a id="error-${code}"></a>
35​
36## Error ${code}: \`${name}\`
37​
38Development builds show the full message. For example:
39​
40${messages[name].map((message) => `\`\`\`text\n${message}\n\`\`\`\n`).join('\n')}`,
42​
43const siteSections = Object.entries(sites).map(
44 ([code, { file, template, kind = 'error' }]) => `<a id="${kind}-${code}"></a>
45​
46## ${kind === 'warning' ? 'Warning' : 'Error'} ${code}: \`${file}\`
47​
48Development builds show this message, with \`\${...}\` replaced by values:
49​
50\`\`\`text
51${template}
52\`\`\`
53`,
55​
56const pagePath = path.join(root, 'docs/errors.md')
57const page = `---
58title: Error Codes
59id: errors
60---
61​
62<!-- Generated by scripts/generate-error-docs.mjs. Do not edit by hand. -->
63​
64Production builds of TanStack DB shorten error messages to a code, the inputs
65that the error can show, and a link to this page. For example:
66​
67\`\`\`text
68TanStack DB error 17 (key=1, collectionId="todos"): https://tanstack.com/db/latest/docs/errors#error-17
69\`\`\`
70​
71A console warning uses the same form with \`warning\` in place of \`error\`.
72Development builds keep the full messages below. The error classes, their
73\`name\` values, and their fields are the same in both builds. See
74[Error Handling](./guides/error-handling.md) for how to catch them.
75​
76${sections.join('\n')}
77## Other errors and log messages
78​
79These are plain \`Error\`, \`TypeError\`, \`RangeError\`, or
80\`AggregateError\` values, and \`console\` messages about runtime failures.
81Each heading names the source file under \`packages/db/src\` that writes it.
82​
83${siteSections.join('\n')}`
84​
85if (mode === '--check') {
86 const current = await readFile(pagePath, 'utf8').catch(() => '')
87 if (current !== page) {
88 console.error(
89 'docs/errors.md is out of date; run node scripts/generate-error-docs.mjs',
90 )
91 process.exit(1)
92 }
93} else if (mode === '--stdout') {
94 process.stdout.write(page)
95} else {
96 await writeFile(pagePath, page)