Skip to content

Commit 85c4adc

Browse files
julienpwardpeet
andauthoredOct 15, 2020
chore(gatsby-cli): Categorize errors (#27449)
* chore(gatsby-cli): Categorize errors * edit message Co-authored-by: Ward Peeters <ward@coding-tech.com>
1 parent b27d755 commit 85c4adc

File tree

5 files changed

+47
-5
lines changed

5 files changed

+47
-5
lines changed
 

‎packages/gatsby-cli/src/reporter/__tests__/__snapshots__/index.ts.snap

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Object {
5353
5454
exports[`report.error handles "structuredError" signature correctly 1`] = `
5555
Object {
56+
"category": "USER",
5657
"code": "95312",
5758
"context": Object {
5859
"ref": "navigator",

‎packages/gatsby-cli/src/reporter/redux/internal-actions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export const createLog = ({
112112
group?: string
113113
code?: string
114114
type?: string
115-
category?: ErrorCategory
115+
category?: keyof typeof ErrorCategory
116116
filePath?: string
117117
location?: IStructuredError["location"]
118118
docsUrl?: string

‎packages/gatsby-cli/src/reporter/redux/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ interface ILog {
4343
group: string | undefined
4444
code: string | undefined
4545
type: string | undefined
46-
category?: ErrorCategory
46+
category?: keyof typeof ErrorCategory
4747
filePath: string | undefined
4848
location: IStructuredError["location"] | undefined
4949
docsUrl: string | undefined

‎packages/gatsby-cli/src/structured-errors/error-map.ts

+43-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const errors = {
2929
`"${context.ref}" is not available during server side rendering.`,
3030
level: Level.ERROR,
3131
docsUrl: `https://gatsby.dev/debug-html`,
32+
category: ErrorCategory.USER,
3233
},
3334
"95313": {
3435
text: (context): string =>
@@ -51,6 +52,7 @@ const errors = {
5152
`${context.stageLabel} failed\n\n${context.sourceMessage}\n\nIf you're trying to use a package make sure that '${context.packageName}' is installed. If you're trying to use a local file make sure that the path is correct.`,
5253
type: Type.WEBPACK,
5354
level: Level.ERROR,
55+
category: ErrorCategory.USER,
5456
},
5557
"85901": {
5658
text: (context): string =>
@@ -78,6 +80,7 @@ const errors = {
7880
},
7981
type: Type.GRAPHQL,
8082
level: Level.ERROR,
83+
category: ErrorCategory.USER,
8184
},
8285
// Deprecated
8386
"85909": {
@@ -105,6 +108,7 @@ const errors = {
105108
type: Type.GRAPHQL,
106109
level: Level.ERROR,
107110
docsUrl: `https://www.gatsbyjs.org/docs/graphql/`,
111+
category: ErrorCategory.USER,
108112
},
109113
"85911": {
110114
text: (context): string =>
@@ -117,6 +121,7 @@ const errors = {
117121
`),
118122
type: Type.GRAPHQL,
119123
level: Level.ERROR,
124+
category: ErrorCategory.USER,
120125
},
121126
"85912": {
122127
text: (context): string =>
@@ -148,6 +153,7 @@ const errors = {
148153
`String interpolation is not allowed in graphql tag:\n\n${context.codeFrame}`,
149154
type: Type.GRAPHQL,
150155
level: Level.ERROR,
156+
category: ErrorCategory.USER,
151157
},
152158
"85917": {
153159
text: (context): string =>
@@ -165,6 +171,7 @@ const errors = {
165171
}`),
166172
type: Type.GRAPHQL,
167173
level: Level.ERROR,
174+
category: ErrorCategory.USER,
168175
},
169176
// Duplicate fragment
170177
"85919": {
@@ -180,6 +187,7 @@ const errors = {
180187
`),
181188
type: Type.GRAPHQL,
182189
level: Level.ERROR,
190+
category: ErrorCategory.USER,
183191
},
184192
// Undefined variables in Queries
185193
"85920": {
@@ -206,24 +214,37 @@ const errors = {
206214
},
207215
type: Type.GRAPHQL,
208216
level: Level.ERROR,
217+
category: ErrorCategory.USER,
209218
},
210219
"85921": {
211220
text: (context): string =>
212221
`There was an error in your GraphQL query:\n\n${context.sourceMessage}\n\nIf you're e.g. filtering for specific nodes make sure that you choose the correct field (that has the same type "${context.inputType}") or adjust the context variable to the type "${context.expectedType}".`,
213222
type: Type.GRAPHQL,
214223
level: Level.ERROR,
224+
category: ErrorCategory.USER,
215225
},
216226
"85922": {
217227
text: (context): string =>
218-
`There was an error in your GraphQL query:\n\n${context.sourceMessage}\n\nThis can happen if you e.g. accidentally added { } to the field "${context.fieldName}". If you didn't expect "${context.fieldName}" to be of type "${context.fieldType}" make sure that your input source and/or plugin is correct.`,
228+
`There was an error in your GraphQL query:
229+
230+
${context.sourceMessage}
231+
232+
This can happen if you e.g. accidentally added { } to the field "${context.fieldName}". If you didn't expect "${context.fieldName}" to be of type "${context.fieldType}" make sure that your input source and/or plugin is correct.
233+
However, if you expect "value" to exist, the field might be accessible in another subfield. Please try your query in GraphiQL and use the GraphiQL explorer to see which fields you can query and what shape they have.
234+
235+
It is recommended to explicitly type your GraphQL schema if you want to use optional fields. This way you don't have to add the mentioned
236+
"dummy content". Visit our docs to learn how you can define the schema for "${context.type}":
237+
https://www.gatsbyjs.org/docs/schema-customization/#creating-type-definitions`,
219238
type: Type.GRAPHQL,
220239
level: Level.ERROR,
240+
category: ErrorCategory.USER,
221241
},
222242
"85923": {
223243
text: (context): string =>
224244
`There was an error in your GraphQL query:\n\n${context.sourceMessage}\n\nIf you don't expect "${context.field}" to exist on the type "${context.type}" it is most likely a typo.\nHowever, if you expect "${context.field}" to exist there are a couple of solutions to common problems:\n\n- If you added a new data source and/or changed something inside gatsby-node.js/gatsby-config.js, please try a restart of your development server\n- The field might be accessible in another subfield, please try your query in GraphiQL and use the GraphiQL explorer to see which fields you can query and what shape they have\n- You want to optionally use your field "${context.field}" and right now it is not used anywhere. Therefore Gatsby can't infer the type and add it to the GraphQL schema. A quick fix is to add at least one entry with that field ("dummy content")\n\nIt is recommended to explicitly type your GraphQL schema if you want to use optional fields. This way you don't have to add the mentioned "dummy content". Visit our docs to learn how you can define the schema for "${context.type}":\nhttps://www.gatsbyjs.org/docs/schema-customization/#creating-type-definitions`,
225245
type: Type.GRAPHQL,
226246
level: Level.ERROR,
247+
category: ErrorCategory.USER,
227248
},
228249
"85924": {
229250
text: (context): string =>
@@ -236,6 +257,7 @@ const errors = {
236257
}".${optionalGraphQLInfo(context)}`,
237258
type: Type.GRAPHQL,
238259
level: Level.ERROR,
260+
category: ErrorCategory.USER,
239261
},
240262
"85925": {
241263
text: (context): string =>
@@ -248,43 +270,50 @@ const errors = {
248270
)}`,
249271
type: Type.GRAPHQL,
250272
level: Level.ERROR,
273+
category: ErrorCategory.USER,
251274
},
252275
"85926": {
253276
text: (context): string =>
254277
`There was an error in your GraphQL query:\n\n${context.sourceMessage}\n\nThis can happen when you used graphql\`{ ...yourQuery }\` instead of graphql(\`{ ...yourQuery }\`) inside gatsby-node.js\n\nYou can't use the template literal function you're used to (from page queries) and rather have to call graphql() as a normal function.`,
255278
type: Type.GRAPHQL,
256279
level: Level.ERROR,
280+
category: ErrorCategory.USER,
257281
},
258282
"85927": {
259283
text: (context): string =>
260284
`There was an error in your GraphQL query:\n\n${context.sourceMessage}\n\nSee if ${context.variable} has a typo or ${context.operation} doesn't actually require this variable.`,
261285
type: Type.GRAPHQL,
262286
level: Level.ERROR,
287+
category: ErrorCategory.USER,
263288
},
264289
// Config errors
265290
"10122": {
266291
text: (context): string =>
267292
`The site's gatsby-config.js failed validation:\n\n${context.sourceMessage}`,
268293
type: Type.CONFIG,
269294
level: Level.ERROR,
295+
category: ErrorCategory.USER,
270296
},
271297
"10123": {
272298
text: (context): string =>
273299
`We encountered an error while trying to load your site's ${context.configName}. Please fix the error and try again.`,
274300
type: Type.CONFIG,
275301
level: Level.ERROR,
302+
category: ErrorCategory.USER,
276303
},
277304
"10124": {
278305
text: (context): string =>
279306
`It looks like you were trying to add the config file? Please rename "${context.nearMatch}" to "${context.configName}.js"`,
280307
type: Type.CONFIG,
281308
level: Level.ERROR,
309+
category: ErrorCategory.USER,
282310
},
283311
"10125": {
284312
text: (context): string =>
285313
`Your ${context.configName} file is in the wrong place. You've placed it in the src/ directory. It must instead be at the root of your site next to your package.json file.`,
286314
type: Type.CONFIG,
287315
level: Level.ERROR,
316+
category: ErrorCategory.USER,
288317
},
289318
"10126": {
290319
text: (context): string =>
@@ -295,6 +324,7 @@ const errors = {
295324
`\nare not supported in the root gatsby-config.`,
296325
type: Type.CONFIG,
297326
level: Level.ERROR,
327+
category: ErrorCategory.USER,
298328
},
299329
"10226": {
300330
text: (context): string =>
@@ -332,6 +362,7 @@ const errors = {
332362
4
333363
)}\n\nSee the documentation for the "createPage" action — https://www.gatsbyjs.org/docs/actions/#createPage`,
334364
level: Level.ERROR,
365+
category: ErrorCategory.USER,
335366
},
336367
"11323": {
337368
text: (context): string =>
@@ -343,6 +374,7 @@ const errors = {
343374
4
344375
)}\n\nSee the documentation for the "createPage" action — https://www.gatsbyjs.org/docs/actions/#createPage`,
345376
level: Level.ERROR,
377+
category: ErrorCategory.USER,
346378
},
347379
"11324": {
348380
text: (context): string =>
@@ -361,6 +393,7 @@ const errors = {
361393
4
362394
)}\n\nSee the documentation for the "createPage" action — https://www.gatsbyjs.org/docs/actions/#createPage`,
363395
level: Level.ERROR,
396+
category: ErrorCategory.USER,
364397
},
365398
"11326": {
366399
text: (context): string =>
@@ -376,16 +409,19 @@ const errors = {
376409
4
377410
)}\n\nSee the documentation for the "createPage" action — https://www.gatsbyjs.org/docs/actions/#createPage`,
378411
level: Level.ERROR,
412+
category: ErrorCategory.USER,
379413
},
380414
"11327": {
381415
text: (context): string =>
382416
`You have an empty file in the "src/pages" directory at "${context.relativePath}". Please remove it or make it a valid component`,
383417
level: Level.ERROR,
418+
category: ErrorCategory.USER,
384419
},
385420
"11328": {
386421
text: (context): string =>
387422
`A page component must export a React component for it to be valid. Please make sure this file exports a React component:\n\n${context.fileName}`,
388423
level: Level.ERROR,
424+
category: ErrorCategory.USER,
389425
},
390426
// invalid or deprecated APIs
391427
"11329": {
@@ -425,6 +461,7 @@ const errors = {
425461
} here: https://www.gatsbyjs.org/docs/node-apis/#${context.api}`,
426462
type: Type.PLUGIN,
427463
level: Level.ERROR,
464+
category: ErrorCategory.USER,
428465
},
429466
// Invalid plugin options
430467
"11331": {
@@ -464,6 +501,7 @@ const errors = {
464501
`for custom ssl --https, --cert-file, and --key-file must be used together`,
465502
level: Level.ERROR,
466503
docsUrl: `https://www.gatsbyjs.org/docs/local-https/#custom-key-and-certificate-files`,
504+
category: ErrorCategory.USER,
467505
},
468506
"11522": {
469507
text: (): string => `Failed to generate dev SSL certificate`,
@@ -476,18 +514,21 @@ const errors = {
476514
`It looks like you gave wrong argument orders . Try running instead "gatsby new ${context.starter} ${context.rootPath}"`,
477515
level: Level.ERROR,
478516
docsUrl: `https://www.gatsbyjs.org/docs/gatsby-cli/#new`,
517+
category: ErrorCategory.USER,
479518
},
480519
"11611": {
481520
text: (context): string =>
482521
`It looks like you passed a URL to your project name. Try running instead "gatsby new new-gatsby-project ${context.rootPath}"`,
483522
level: Level.ERROR,
484523
docsUrl: `https://www.gatsbyjs.org/docs/gatsby-cli/#new`,
524+
category: ErrorCategory.USER,
485525
},
486526
"11612": {
487527
text: (context): string =>
488528
`Could not create a project in "${context.path}" because it's not a valid path`,
489529
level: Level.ERROR,
490530
docsUrl: `https://www.gatsbyjs.org/docs/gatsby-cli/#new`,
531+
category: ErrorCategory.USER,
491532
},
492533
"11613": {
493534
text: (context): string =>
@@ -508,6 +549,6 @@ export interface IErrorMapEntry {
508549
// keyof typeof is used for these enums so that the public facing API (e.g. used by setErrorMap) doesn't rely on enum but gives an union
509550
level: keyof typeof Level
510551
type?: keyof typeof Type
511-
category?: ErrorCategory
552+
category?: keyof typeof ErrorCategory
512553
docsUrl?: string
513554
}

‎packages/gatsby-cli/src/structured-errors/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export interface IStructuredError {
3030
start: ILocationPosition
3131
end?: ILocationPosition
3232
}
33-
category?: ErrorCategory
33+
category?: keyof typeof ErrorCategory
3434
error?: Error
3535
group?: string
3636
level: IErrorMapEntry["level"]

0 commit comments

Comments
 (0)
Please sign in to comment.