Skip to content

Commit 58d5f2a

Browse files
authored
fix(gatsby-plugin-mdx): Better AST error (#36866)
1 parent 2b8c2f2 commit 58d5f2a

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

packages/gatsby-plugin-mdx/src/error-utils.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,24 @@ export const ERROR_CODES = {
99
export const ERROR_MAP = {
1010
[ERROR_CODES.MdxCompilation]: {
1111
text: (context: { absolutePath: string; errorMeta: any }): string =>
12-
`Failed to compile the file "${context.absolutePath}". Original error message: \n\n${context.errorMeta.message}`,
12+
`Failed to compile the file "${context.absolutePath}". Original error message:\n\n${context.errorMeta.message}`,
1313
level: `ERROR`,
1414
type: `PLUGIN`,
1515
category: `USER`,
1616
},
1717
[ERROR_CODES.NonExistentFileNode]: {
18-
text: (context: { resourcePath: string }): string =>
19-
`Unable to locate the GraphQL File node for ${context.resourcePath}`,
18+
text: (context: { resourcePath: string; mdxPath?: string }): string =>
19+
`Unable to locate the GraphQL File node for ${context.resourcePath}${
20+
context.mdxPath ? `\nFile: ${context.mdxPath}` : ``
21+
}`,
2022
level: `ERROR`,
2123
type: `PLUGIN`,
2224
},
2325
[ERROR_CODES.InvalidAcornAST]: {
24-
text: (context: { resourcePath: string }): string =>
25-
`Invalid AST. Parsed source code did not return valid output. File:\n${context.resourcePath}`,
26+
text: (context: { resourcePath: string; mdxPath?: string }): string =>
27+
`Invalid AST. Parsed source code did not return valid output.\n\nTemplate:\n${
28+
context.resourcePath
29+
}${context.mdxPath ? `\nFile: ${context.mdxPath}` : ``}`,
2630
level: `ERROR`,
2731
type: `PLUGIN`,
2832
category: `USER`,

packages/gatsby-plugin-mdx/src/gatsby-layout-loader.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const gatsbyLayoutLoader: LoaderDefinition = async function (
3939
id: ERROR_CODES.NonExistentFileNode,
4040
context: {
4141
resourcePath: this.resourcePath,
42+
mdxPath,
4243
},
4344
})
4445
}
@@ -71,6 +72,7 @@ const gatsbyLayoutLoader: LoaderDefinition = async function (
7172
id: ERROR_CODES.InvalidAcornAST,
7273
context: {
7374
resourcePath: this.resourcePath,
75+
mdxPath,
7476
},
7577
})
7678
}
@@ -249,6 +251,7 @@ const gatsbyLayoutLoader: LoaderDefinition = async function (
249251
id: ERROR_CODES.InvalidAcornAST,
250252
context: {
251253
resourcePath: this.resourcePath,
254+
mdxPath,
252255
},
253256
error,
254257
})

0 commit comments

Comments
 (0)