Skip to content

Commit

Permalink
fix(gatsby): show stack trace for non-graphql errors (#28888)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pvdz authored Jan 7, 2021
1 parent ef12dca commit 1769fc3
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/gatsby/src/bootstrap/create-graphql-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export const createGraphQLRunner = (
},
},
filePath: file.getFileName(),
error: e,
})
structuredError.context = {
...structuredError.context,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`query-error-parser real error 1`] = `
Object {
"context": Object {
"sourceMessage": "this error should show a trace",
},
"error": [Error: this error should show a trace],
"filePath": "test.js",
"id": "85901",
"location": Object {
"start": Object {
"column": 10,
"line": 5,
},
},
}
`;

exports[`query-error-parser specific one 1`] = `
Object {
"context": Object {
Expand Down
9 changes: 8 additions & 1 deletion packages/gatsby/src/query/__tests__/error-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ describe(`query-error-parser`, () => {
`85920`,
],
[`totally vague one`, `foo bar`, `85901`],
])(`%s`, (_name, message, expectedId) => {
[
`real error`,
`this error should show a trace`,
`85901`,
new Error(`this error should show a trace`),
],
])(`%s`, (_name, message, expectedId, error = undefined) => {
const structured = errorParser({
message,
filePath: `test.js`,
location: { start: { line: 5, column: 10 } },
error: error instanceof Error ? error : undefined,
})
expect(structured).toMatchSnapshot()
expect(structured.id).toEqual(expectedId)
Expand Down
4 changes: 4 additions & 0 deletions packages/gatsby/src/query/__tests__/query-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ describe(`actual compiling`, () => {
| ^
12 | }",
},
"error": [GraphQLError: Cannot spread fragment "Foo" within itself via Bar.],
"filePath": "mockFile",
"id": "85901",
"location": Any<Object>,
Expand Down Expand Up @@ -814,6 +815,7 @@ describe(`actual compiling`, () => {
| ^
5 | }",
},
"error": [GraphQLError: Fragment "PostsJsonFragment" cannot be spread here as objects of type "PostsJson" can never be of type "PostsJsonConnection".],
"filePath": "mockFile",
"id": "85901",
"location": Object {
Expand Down Expand Up @@ -900,6 +902,7 @@ describe(`actual compiling`, () => {
"context": Object {
"sourceMessage": "This anonymous operation must be the only defined operation.",
},
"error": [GraphQLError: This anonymous operation must be the only defined operation.],
"filePath": "mockFile",
"id": "85901",
"location": Object {
Expand Down Expand Up @@ -1055,6 +1058,7 @@ describe(`actual compiling`, () => {
| ^
2 | field",
},
"error": [GraphQLError: Unknown type "ThisTypeSurelyDoesntExistInSchema".],
"filePath": "mockFile",
"id": "85901",
"location": Object {
Expand Down
16 changes: 13 additions & 3 deletions packages/gatsby/src/query/error-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ interface IErrorParser {
end?: SourceLocation
}
| undefined
error?: Error
}

const errorParser = ({
message,
filePath = undefined,
location = undefined,
error = undefined,
}: IErrorParser): IMatch => {
// Handle GraphQL errors. A list of regexes to match certain
// errors to specific callbacks
Expand Down Expand Up @@ -128,9 +130,17 @@ const errorParser = ({
{
regex: /[\s\S]*/gm,
cb: (match): IMatch => {
return {
id: `85901`,
context: { sourceMessage: match[0] },
if (error instanceof Error) {
return {
id: `85901`,
error, // show stack trace
context: { sourceMessage: match[0] },
}
} else {
return {
id: `85901`,
context: { sourceMessage: match[0] },
}
}
},
},
Expand Down
8 changes: 7 additions & 1 deletion packages/gatsby/src/query/query-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,12 @@ const extractOperations = (schema, parsedQueries, addError, parentSpan) => {
const location = {
start: locInGraphQlToLocInFile(templateLoc, error.locations[0]),
}
return errorParser({ message: error.message, filePath, location })
return errorParser({
message: error.message,
filePath,
location,
error,
})
})
)

Expand Down Expand Up @@ -384,6 +389,7 @@ const processDefinitions = ({
},
message,
filePath,
error,
})
)
}
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/src/query/query-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function panicQueryJobError(
message: e.message,
filePath: undefined,
location: undefined,
error: e,
})

structuredError.context = {
Expand Down

0 comments on commit 1769fc3

Please sign in to comment.