Skip to content

Commit 621b4e3

Browse files
committed
imporve mdx loader errors
1 parent 48df4f1 commit 621b4e3

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

packages/docusaurus-mdx-loader/src/loader.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,21 @@ export async function mdxLoader(
162162
result = await processor.process({content, filePath});
163163
} catch (errorUnknown) {
164164
const error = errorUnknown as Error;
165+
166+
// MDX can emit errors that have useful extra attributes
167+
const errorJSON = JSON.stringify(error, null, 2);
168+
const errorDetails =
169+
errorJSON === '{}'
170+
? // regular JS error case: print stacktrace
171+
error.stack ?? 'N/A'
172+
: // MDX error: print extra attributes + stacktrace
173+
`${errorJSON}\n${error.stack}`;
174+
165175
return callback(
166176
new Error(
167177
`MDX compilation failed for file ${logger.path(filePath)}\nCause: ${
168178
error.message
169-
}\nDetails:\n${JSON.stringify(error, null, 2)}`,
179+
}\nDetails:\n${errorDetails}`,
170180
// TODO error cause doesn't seem to be used by Webpack stats.errors :s
171181
{cause: error},
172182
),

0 commit comments

Comments
 (0)