Skip to content

Commit

Permalink
imporve mdx loader errors
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Jun 22, 2023
1 parent 48df4f1 commit 621b4e3
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/docusaurus-mdx-loader/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,21 @@ export async function mdxLoader(
result = await processor.process({content, filePath});
} catch (errorUnknown) {
const error = errorUnknown as Error;

// MDX can emit errors that have useful extra attributes
const errorJSON = JSON.stringify(error, null, 2);
const errorDetails =
errorJSON === '{}'
? // regular JS error case: print stacktrace
error.stack ?? 'N/A'
: // MDX error: print extra attributes + stacktrace
`${errorJSON}\n${error.stack}`;

return callback(
new Error(
`MDX compilation failed for file ${logger.path(filePath)}\nCause: ${
error.message
}\nDetails:\n${JSON.stringify(error, null, 2)}`,
}\nDetails:\n${errorDetails}`,
// TODO error cause doesn't seem to be used by Webpack stats.errors :s
{cause: error},
),
Expand Down

0 comments on commit 621b4e3

Please sign in to comment.