Skip to content

Commit 689d31e

Browse files
committed
TypeScript: Improve error message
1 parent b2704eb commit 689d31e

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

.changeset/honest-ravens-heal.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'fumadocs-typescript': patch
3+
'fumadocs-mdx': patch
4+
---
5+
6+
Improve error message

packages/mdx/src/loaders/adapter.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type { SourceMap, TransformPluginContext } from 'rollup';
66
import type { TransformResult } from 'vite';
77
import { parse } from 'node:querystring';
88
import { ValidationError } from '@/utils/validation';
9-
import path from 'node:path';
109
import type { LoaderContext } from 'webpack';
1110
import { readFileSync } from 'node:fs';
1211

@@ -165,9 +164,6 @@ export function toWebpack(loader: Loader): WebpackLoader {
165164
}
166165

167166
if (!(error instanceof Error)) throw error;
168-
169-
const fpath = path.relative(this.context, this.resourcePath);
170-
error.message = `${fpath}:${error.name}: ${error.message}`;
171167
callback(error);
172168
}
173169
};

packages/typescript/src/lib/remark-auto-type-table.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -215,21 +215,33 @@ export function remarkAutoTypeTable(
215215

216216
visit(tree, 'mdxJsxFlowElement', (node) => {
217217
if (node.name !== name) return;
218-
const props: Record<string, string | null> = {};
218+
const props: Record<string, string | true> = {};
219+
const onError = (message: string, cause?: Error) => {
220+
const location = node.position
221+
? `${file.path}:${node.position.start.line}:${node.position.start.column}`
222+
: file.path;
223+
throw new Error(`${location} from <auto-type-table>: ${message}`, {
224+
cause,
225+
});
226+
};
219227

220228
for (const attr of node.attributes) {
221-
if (
222-
attr.type !== 'mdxJsxAttribute' ||
223-
(typeof attr.value !== 'string' && attr.value !== null)
224-
)
225-
throw new Error(
226-
'`auto-type-table` only support string & boolean attributes',
227-
);
228-
229-
props[attr.name] = attr.value;
229+
if (attr.type !== 'mdxJsxAttribute') {
230+
onError('only named attributes are allowed.');
231+
} else if (typeof attr.value === 'string') {
232+
props[attr.name] = attr.value;
233+
} else if (attr.value === null) {
234+
props[attr.name] = true;
235+
} else {
236+
onError('only string & boolean attributes are allowed.');
237+
}
230238
}
231239

232-
queue.push(run(node, props));
240+
queue.push(
241+
run(node, props).catch((err) => {
242+
onError('failed to generate type table', err);
243+
}),
244+
);
233245
return 'skip';
234246
});
235247

0 commit comments

Comments
 (0)