TypeScript: Improve error message

This commit is contained in:
Fuma Nama
2025-12-30 16:34:33 +08:00
parent b2704eb0b4
commit 689d31ec08
3 changed files with 29 additions and 15 deletions
+6
View File
@@ -0,0 +1,6 @@
---
'fumadocs-typescript': patch
'fumadocs-mdx': patch
---
Improve error message
-4
View File
@@ -6,7 +6,6 @@ import type { SourceMap, TransformPluginContext } from 'rollup';
import type { TransformResult } from 'vite';
import { parse } from 'node:querystring';
import { ValidationError } from '@/utils/validation';
import path from 'node:path';
import type { LoaderContext } from 'webpack';
import { readFileSync } from 'node:fs';
@@ -165,9 +164,6 @@ export function toWebpack(loader: Loader): WebpackLoader {
}
if (!(error instanceof Error)) throw error;
const fpath = path.relative(this.context, this.resourcePath);
error.message = `${fpath}:${error.name}: ${error.message}`;
callback(error);
}
};
@@ -215,21 +215,33 @@ export function remarkAutoTypeTable(
visit(tree, 'mdxJsxFlowElement', (node) => {
if (node.name !== name) return;
const props: Record<string, string | null> = {};
const props: Record<string, string | true> = {};
const onError = (message: string, cause?: Error) => {
const location = node.position
? `${file.path}:${node.position.start.line}:${node.position.start.column}`
: file.path;
throw new Error(`${location} from <auto-type-table>: ${message}`, {
cause,
});
};
for (const attr of node.attributes) {
if (
attr.type !== 'mdxJsxAttribute' ||
(typeof attr.value !== 'string' && attr.value !== null)
)
throw new Error(
'`auto-type-table` only support string & boolean attributes',
);
props[attr.name] = attr.value;
if (attr.type !== 'mdxJsxAttribute') {
onError('only named attributes are allowed.');
} else if (typeof attr.value === 'string') {
props[attr.name] = attr.value;
} else if (attr.value === null) {
props[attr.name] = true;
} else {
onError('only string & boolean attributes are allowed.');
}
}
queue.push(run(node, props));
queue.push(
run(node, props).catch((err) => {
onError('failed to generate type table', err);
}),
);
return 'skip';
});