Skip to content

Commit 1769fc3

Browse files
authored
fix(gatsby): show stack trace for non-graphql errors (#28888)
* fix(gatsby): show stack trace for non-graphql errors * If we want to squash more graphql specific errors we should catch them * one attempt at fixing snapshots * Drop unused imported type
1 parent ef12dca commit 1769fc3

File tree

7 files changed

+51
-5
lines changed

7 files changed

+51
-5
lines changed

packages/gatsby/src/bootstrap/create-graphql-runner.ts

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export const createGraphQLRunner = (
7979
},
8080
},
8181
filePath: file.getFileName(),
82+
error: e,
8283
})
8384
structuredError.context = {
8485
...structuredError.context,

packages/gatsby/src/query/__tests__/__snapshots__/error-parser.ts.snap

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`query-error-parser real error 1`] = `
4+
Object {
5+
"context": Object {
6+
"sourceMessage": "this error should show a trace",
7+
},
8+
"error": [Error: this error should show a trace],
9+
"filePath": "test.js",
10+
"id": "85901",
11+
"location": Object {
12+
"start": Object {
13+
"column": 10,
14+
"line": 5,
15+
},
16+
},
17+
}
18+
`;
19+
320
exports[`query-error-parser specific one 1`] = `
421
Object {
522
"context": Object {

packages/gatsby/src/query/__tests__/error-parser.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@ describe(`query-error-parser`, () => {
88
`85920`,
99
],
1010
[`totally vague one`, `foo bar`, `85901`],
11-
])(`%s`, (_name, message, expectedId) => {
11+
[
12+
`real error`,
13+
`this error should show a trace`,
14+
`85901`,
15+
new Error(`this error should show a trace`),
16+
],
17+
])(`%s`, (_name, message, expectedId, error = undefined) => {
1218
const structured = errorParser({
1319
message,
1420
filePath: `test.js`,
1521
location: { start: { line: 5, column: 10 } },
22+
error: error instanceof Error ? error : undefined,
1623
})
1724
expect(structured).toMatchSnapshot()
1825
expect(structured.id).toEqual(expectedId)

packages/gatsby/src/query/__tests__/query-compiler.js

+4
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ describe(`actual compiling`, () => {
452452
| ^
453453
12 | }",
454454
},
455+
"error": [GraphQLError: Cannot spread fragment "Foo" within itself via Bar.],
455456
"filePath": "mockFile",
456457
"id": "85901",
457458
"location": Any<Object>,
@@ -814,6 +815,7 @@ describe(`actual compiling`, () => {
814815
| ^
815816
5 | }",
816817
},
818+
"error": [GraphQLError: Fragment "PostsJsonFragment" cannot be spread here as objects of type "PostsJson" can never be of type "PostsJsonConnection".],
817819
"filePath": "mockFile",
818820
"id": "85901",
819821
"location": Object {
@@ -900,6 +902,7 @@ describe(`actual compiling`, () => {
900902
"context": Object {
901903
"sourceMessage": "This anonymous operation must be the only defined operation.",
902904
},
905+
"error": [GraphQLError: This anonymous operation must be the only defined operation.],
903906
"filePath": "mockFile",
904907
"id": "85901",
905908
"location": Object {
@@ -1055,6 +1058,7 @@ describe(`actual compiling`, () => {
10551058
| ^
10561059
2 | field",
10571060
},
1061+
"error": [GraphQLError: Unknown type "ThisTypeSurelyDoesntExistInSchema".],
10581062
"filePath": "mockFile",
10591063
"id": "85901",
10601064
"location": Object {

packages/gatsby/src/query/error-parser.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ interface IErrorParser {
1010
end?: SourceLocation
1111
}
1212
| undefined
13+
error?: Error
1314
}
1415

1516
const errorParser = ({
1617
message,
1718
filePath = undefined,
1819
location = undefined,
20+
error = undefined,
1921
}: IErrorParser): IMatch => {
2022
// Handle GraphQL errors. A list of regexes to match certain
2123
// errors to specific callbacks
@@ -128,9 +130,17 @@ const errorParser = ({
128130
{
129131
regex: /[\s\S]*/gm,
130132
cb: (match): IMatch => {
131-
return {
132-
id: `85901`,
133-
context: { sourceMessage: match[0] },
133+
if (error instanceof Error) {
134+
return {
135+
id: `85901`,
136+
error, // show stack trace
137+
context: { sourceMessage: match[0] },
138+
}
139+
} else {
140+
return {
141+
id: `85901`,
142+
context: { sourceMessage: match[0] },
143+
}
134144
}
135145
},
136146
},

packages/gatsby/src/query/query-compiler.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,12 @@ const extractOperations = (schema, parsedQueries, addError, parentSpan) => {
217217
const location = {
218218
start: locInGraphQlToLocInFile(templateLoc, error.locations[0]),
219219
}
220-
return errorParser({ message: error.message, filePath, location })
220+
return errorParser({
221+
message: error.message,
222+
filePath,
223+
location,
224+
error,
225+
})
221226
})
222227
)
223228

@@ -384,6 +389,7 @@ const processDefinitions = ({
384389
},
385390
message,
386391
filePath,
392+
error,
387393
})
388394
)
389395
}

packages/gatsby/src/query/query-runner.ts

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ function panicQueryJobError(
6363
message: e.message,
6464
filePath: undefined,
6565
location: undefined,
66+
error: e,
6667
})
6768

6869
structuredError.context = {

0 commit comments

Comments
 (0)