Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby-cli): Augment plugin errors with plugin name #27435

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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