Skip to content

Commit

Permalink
feat(gatsby-cli): Augment plugin errors with plugin name (#27435)
Browse files Browse the repository at this point in the history
* feat(gatsby-cli): Augment plugin errors with plugin name

* Add plugin name for unstructured errors too

* Refactoring

Co-authored-by: gatsbybot <mathews.kyle+gatsbybot@gmail.com>
Co-authored-by: Sidhartha Chatterjee <me@sidharthachatterjee.com>
  • Loading branch information
3 people committed Nov 6, 2020
1 parent 051e1aa commit 700d245
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
3 changes: 3 additions & 0 deletions packages/gatsby-cli/src/reporter/redux/internal-actions.ts
Expand Up @@ -104,6 +104,7 @@ export const createLog = ({
activity_type,
activity_uuid,
stack,
pluginName,
}: {
level: string
text?: string
Expand All @@ -122,6 +123,7 @@ export const createLog = ({
activity_type?: string
activity_uuid?: string
stack?: IStructuredError["stack"]
pluginName?: string
}): ICreateLog => {
return {
type: Actions.Log,
Expand All @@ -144,6 +146,7 @@ export const createLog = ({
activity_uuid,
timestamp: new Date().toJSON(),
stack,
pluginName,
},
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-cli/src/reporter/redux/types.ts
Expand Up @@ -54,6 +54,7 @@ interface ILog {
activity_uuid: string | undefined
timestamp: string
stack: IStructuredError["stack"] | undefined
pluginName: string | undefined
}

export interface ICreateLog {
Expand Down
7 changes: 4 additions & 3 deletions packages/gatsby-cli/src/structured-errors/error-map.ts
Expand Up @@ -17,9 +17,10 @@ export enum ErrorCategory {
const errors = {
"": {
text: (context): string => {
const sourceMessage = context.sourceMessage
? context.sourceMessage
: `There was an error`
const sourceMessage =
context && context.sourceMessage
? context.sourceMessage
: `There was an error`
return sourceMessage
},
level: Level.ERROR,
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-cli/src/structured-errors/error-schema.ts
Expand Up @@ -36,5 +36,6 @@ export const errorSchema: Joi.ObjectSchema<IStructuredError> = Joi.object().keys
context: Joi.object({}).unknown(),
group: Joi.string(),
panicOnBuild: Joi.boolean(),
pluginName: Joi.string(),
}
)
50 changes: 42 additions & 8 deletions packages/gatsby/src/utils/api-runner-node.js
Expand Up @@ -144,7 +144,7 @@ function extendErrorIdWithPluginName(pluginName, errorMeta) {
return errorMeta
}

function getErrorMapWthPluginName(pluginName, errorMap) {
function getErrorMapWithPluginName(pluginName, errorMap) {
const entries = Object.entries(errorMap)

return entries.reduce((memo, [key, val]) => {
Expand All @@ -165,21 +165,55 @@ function extendLocalReporterToCatchPluginErrors({
let panic = reporter.panic
let panicOnBuild = reporter.panicOnBuild

const addPluginNameToErrorMeta = (errorMeta, pluginName) =>
typeof errorMeta === `string`
? {
context: {
sourceMessage: errorMeta,
},
pluginName,
}
: {
...errorMeta,
pluginName,
}

if (pluginName && reporter?.setErrorMap) {
setErrorMap = errorMap =>
reporter.setErrorMap(getErrorMapWthPluginName(pluginName, errorMap))
reporter.setErrorMap(getErrorMapWithPluginName(pluginName, errorMap))

error = (errorMeta, error) =>
reporter.error(extendErrorIdWithPluginName(pluginName, errorMeta), error)
error = (errorMeta, error) => {
const errorMetaWithPluginName = addPluginNameToErrorMeta(
errorMeta,
pluginName
)
reporter.error(
extendErrorIdWithPluginName(pluginName, errorMetaWithPluginName),
error
)
}

panic = (errorMeta, error) =>
reporter.panic(extendErrorIdWithPluginName(pluginName, errorMeta), error)
panic = (errorMeta, error) => {
const errorMetaWithPluginName = addPluginNameToErrorMeta(
errorMeta,
pluginName
)
reporter.panic(
extendErrorIdWithPluginName(pluginName, errorMetaWithPluginName),
error
)
}

panicOnBuild = (errorMeta, error) =>
panicOnBuild = (errorMeta, error) => {
const errorMetaWithPluginName = addPluginNameToErrorMeta(
errorMeta,
pluginName
)
reporter.panicOnBuild(
extendErrorIdWithPluginName(pluginName, errorMeta),
extendErrorIdWithPluginName(pluginName, errorMetaWithPluginName),
error
)
}
}

return {
Expand Down

0 comments on commit 700d245

Please sign in to comment.