Skip to content

Commit

Permalink
fix(gatsby,gatsby-cli): Correct behavior for reporter.error with plug…
Browse files Browse the repository at this point in the history
…inName (#30331)

Co-authored-by: gatsbybot <mathews.kyle+gatsbybot@gmail.com>
  • Loading branch information
LekoArts and gatsbybot authored Mar 23, 2021
1 parent a0826b9 commit eb1e2d8
Show file tree
Hide file tree
Showing 8 changed files with 447 additions and 229 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,51 @@ Object {
}
`;
exports[`report.error handles "String, Error, pluginName" signature correctly 1`] = `
Object {
"context": Object {
"sourceMessage": "Error string passed to reporter Message from new Error",
},
"docsUrl": "https://gatsby.dev/issue-how-to",
"error": [Error: Message from new Error],
"level": "ERROR",
"pluginName": "gatsby-plugin-foo-bar",
"stack": Array [
Object {
"columnNumber": 7,
"fileName": "<PROJECT_ROOT>/packages/gatsby-cli/src/reporter/__tests__/index.ts",
"functionName": null,
"lineNumber": 100,
},
Object {
"columnNumber": 37,
"fileName": "<PROJECT_ROOT>/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js",
"functionName": "Object.asyncJestTest",
"lineNumber": 102,
},
Object {
"columnNumber": 12,
"fileName": "<PROJECT_ROOT>/node_modules/jest-jasmine2/build/queueRunner.js",
"functionName": null,
"lineNumber": 43,
},
Object {
"columnNumber": 19,
"fileName": "<PROJECT_ROOT>/node_modules/jest-jasmine2/build/queueRunner.js",
"functionName": "mapper",
"lineNumber": 26,
},
Object {
"columnNumber": 41,
"fileName": "<PROJECT_ROOT>/node_modules/jest-jasmine2/build/queueRunner.js",
"functionName": null,
"lineNumber": 73,
},
],
"text": "Error string passed to reporter Message from new Error",
}
`;
exports[`report.error handles "structuredError" signature correctly 1`] = `
Object {
"category": "USER",
Expand All @@ -64,3 +109,30 @@ Object {
"text": "\\"navigator\\" is not available during server side rendering.",
}
`;
exports[`report.error uses custom error from errorMap 1`] = `
Object {
"code": "1337",
"context": Object {
"someProp": "test123",
},
"docsUrl": "https://www.gatsbyjs.org/docs/gatsby-cli/#new",
"level": "ERROR",
"stack": Array [],
"text": "Error text is test123",
}
`;
exports[`report.error uses custom error from errorMap with pluginName 1`] = `
Object {
"code": "gatsby-plugin-foo-bar_1337",
"context": Object {
"someProp": "test123",
},
"docsUrl": "https://www.gatsbyjs.org/docs/gatsby-cli/#new",
"level": "ERROR",
"pluginName": "gatsby-plugin-foo-bar",
"stack": Array [],
"text": "Error text is test123",
}
`;
62 changes: 61 additions & 1 deletion packages/gatsby-cli/src/reporter/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,75 @@ describe(`report.error`, () => {
expect(generatedError).toMatchSnapshot()
})

it(`handles "String, Error, pluginName" signature correctly`, () => {
reporter.error(
`Error string passed to reporter`,
new Error(`Message from new Error`),
`gatsby-plugin-foo-bar`
)
const generatedError = getErrorMessages(
reporterActions.createLog as jest.Mock
)[0]
expect(generatedError).toMatchSnapshot()
})

it(`sets an error map if setErrorMap is called`, () => {
reporter.setErrorMap({
"1337": {
text: (context): string => `Error text is ${context.someProp} `,
text: (context): string => `Error text is ${context.someProp}`,
level: Level.ERROR,
docsUrl: `https://www.gatsbyjs.org/docs/gatsby-cli/#new`,
},
})

expect(reporter.errorMap[`1337`]).toBeTruthy()
})

it(`uses custom error from errorMap`, () => {
reporter.setErrorMap({
"1337": {
text: (context): string => `Error text is ${context.someProp}`,
level: Level.ERROR,
docsUrl: `https://www.gatsbyjs.org/docs/gatsby-cli/#new`,
},
})

reporter.error({
id: `1337`,
context: {
someProp: `test123`,
},
})
const generatedError = getErrorMessages(
reporterActions.createLog as jest.Mock
)[0]
expect(generatedError).toMatchSnapshot()
})

// This is how it's potentially called from api-runner-node.js
// It'll prefix the errorMap and then pass the pluginName as third arg
it(`uses custom error from errorMap with pluginName`, () => {
reporter.setErrorMap({
"gatsby-plugin-foo-bar_1337": {
text: (context): string => `Error text is ${context.someProp}`,
level: Level.ERROR,
docsUrl: `https://www.gatsbyjs.org/docs/gatsby-cli/#new`,
},
})

reporter.error(
{
id: `1337`,
context: {
someProp: `test123`,
},
},
undefined,
`gatsby-plugin-foo-bar`
)
const generatedError = getErrorMessages(
reporterActions.createLog as jest.Mock
)[0]
expect(generatedError).toMatchSnapshot()
})
})
35 changes: 25 additions & 10 deletions packages/gatsby-cli/src/reporter/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getErrorFormatter } from "./errors"
import constructError from "../structured-errors/construct-error"
import { IErrorMapEntry, ErrorId } from "../structured-errors/error-map"
import { prematureEnd } from "./catch-exit-signals"
import { IStructuredError } from "../structured-errors/types"
import { IConstructError, IStructuredError } from "../structured-errors/types"
import { createTimerReporter, ITimerReporter } from "./reporter-timer"
import { createPhantomReporter, IPhantomReporter } from "./reporter-phantom"
import { createProgressReporter, IProgressReporter } from "./reporter-progress"
Expand Down Expand Up @@ -83,18 +83,23 @@ class Reporter {
/**
* Log arguments and exit process with status 1.
*/
panic = (errorMeta: ErrorMeta, error?: Error | Array<Error>): never => {
const reporterError = this.error(errorMeta, error)
panic = (
errorMeta: ErrorMeta,
error?: Error | Array<Error>,
pluginName?: string
): never => {
const reporterError = this.error(errorMeta, error, pluginName)
trackError(`GENERAL_PANIC`, { error: reporterError })
prematureEnd()
return process.exit(1)
}

panicOnBuild = (
errorMeta: ErrorMeta,
error?: Error | Array<Error>
error?: Error | Array<Error>,
pluginName?: string
): IStructuredError | Array<IStructuredError> => {
const reporterError = this.error(errorMeta, error)
const reporterError = this.error(errorMeta, error, pluginName)
trackError(`BUILD_PANIC`, { error: reporterError })
if (process.env.gatsby_executing_command === `build`) {
prematureEnd()
Expand All @@ -105,12 +110,10 @@ class Reporter {

error = (
errorMeta: ErrorMeta | Array<ErrorMeta>,
error?: Error | Array<Error>
error?: Error | Array<Error>,
pluginName?: string
): IStructuredError | Array<IStructuredError> => {
let details: {
error?: Error
context: Record<string, string>
} = {
let details: IConstructError["details"] = {
context: {},
}

Expand Down Expand Up @@ -154,6 +157,18 @@ class Reporter {
}
}

if (pluginName) {
details.pluginName = pluginName
const id = details?.id

if (id) {
const isPrefixed = id.includes(`${pluginName}_`)
if (!isPrefixed) {
details.id = `${pluginName}_${id}`
}
}
}

const structuredError = constructError({ details }, this.errorMap)

if (structuredError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ test(`it defaults to generic error`, () => {
})
)

expect(defaultError.text({})).toEqual(`There was an error`)
expect(defaultError.text({})).toEqual(
`There was an unhandled error and we could not retrieve more information. Please run the command with the --verbose flag again.`
)
})

test(`it supports structured lookups`, () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-cli/src/structured-errors/error-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const errors = {
const sourceMessage =
context && context.sourceMessage
? context.sourceMessage
: `There was an error`
: `There was an unhandled error and we could not retrieve more information. Please run the command with the --verbose flag again.`
return sourceMessage
},
level: Level.ERROR,
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby-cli/src/structured-errors/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface IConstructError {
id?: ErrorId
context?: Record<string, string>
error?: Error
pluginName?: string
[key: string]: unknown
}
}
Expand Down Expand Up @@ -36,6 +37,7 @@ export interface IStructuredError {
level: IErrorMapEntry["level"]
type?: IErrorMapEntry["type"]
docsUrl?: string
pluginName?: string
}

export interface IOptionalGraphQLInfoContext {
Expand Down
Loading

0 comments on commit eb1e2d8

Please sign in to comment.