From 9152f7fd76cd1898d111889df97b3d502a958cd3 Mon Sep 17 00:00:00 2001 From: Julien Poissonnier Date: Wed, 14 Oct 2020 09:29:45 +0200 Subject: [PATCH 1/3] feat(gatsby-cli): Augment plugin errors with plugin name --- .../src/reporter/redux/internal-actions.ts | 3 +++ packages/gatsby-cli/src/reporter/redux/types.ts | 1 + .../src/structured-errors/error-schema.ts | 1 + packages/gatsby/src/utils/api-runner-node.js | 16 +++++++++++----- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/gatsby-cli/src/reporter/redux/internal-actions.ts b/packages/gatsby-cli/src/reporter/redux/internal-actions.ts index dd2b303ef3adb..b78d63789c5d2 100644 --- a/packages/gatsby-cli/src/reporter/redux/internal-actions.ts +++ b/packages/gatsby-cli/src/reporter/redux/internal-actions.ts @@ -104,6 +104,7 @@ export const createLog = ({ activity_type, activity_uuid, stack, + pluginName, }: { level: string text?: string @@ -122,6 +123,7 @@ export const createLog = ({ activity_type?: string activity_uuid?: string stack?: IStructuredError["stack"] + pluginName?: string }): ICreateLog => { return { type: Actions.Log, @@ -144,6 +146,7 @@ export const createLog = ({ activity_uuid, timestamp: new Date().toJSON(), stack, + pluginName, }, } } diff --git a/packages/gatsby-cli/src/reporter/redux/types.ts b/packages/gatsby-cli/src/reporter/redux/types.ts index 31d1f1c6ddbf8..b76abe5033c13 100644 --- a/packages/gatsby-cli/src/reporter/redux/types.ts +++ b/packages/gatsby-cli/src/reporter/redux/types.ts @@ -54,6 +54,7 @@ interface ILog { activity_uuid: string | undefined timestamp: string stack: IStructuredError["stack"] | undefined + pluginName: string | undefined } export interface ICreateLog { diff --git a/packages/gatsby-cli/src/structured-errors/error-schema.ts b/packages/gatsby-cli/src/structured-errors/error-schema.ts index 42ac9b035b3f0..873ba4496f8ee 100644 --- a/packages/gatsby-cli/src/structured-errors/error-schema.ts +++ b/packages/gatsby-cli/src/structured-errors/error-schema.ts @@ -36,5 +36,6 @@ export const errorSchema: Joi.ObjectSchema = Joi.object().keys context: Joi.object({}).unknown(), group: Joi.string(), panicOnBuild: Joi.boolean(), + pluginName: Joi.string(), } ) diff --git a/packages/gatsby/src/utils/api-runner-node.js b/packages/gatsby/src/utils/api-runner-node.js index 43ca6a498ef8c..c75c806a9fe96 100644 --- a/packages/gatsby/src/utils/api-runner-node.js +++ b/packages/gatsby/src/utils/api-runner-node.js @@ -143,7 +143,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]) => { @@ -166,17 +166,23 @@ function extendLocalReporterToCatchPluginErrors({ 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) + reporter.error( + extendErrorIdWithPluginName(pluginName, { ...errorMeta, pluginName }), + error + ) panic = (errorMeta, error) => - reporter.panic(extendErrorIdWithPluginName(pluginName, errorMeta), error) + reporter.panic( + extendErrorIdWithPluginName(pluginName, { ...errorMeta, pluginName }), + error + ) panicOnBuild = (errorMeta, error) => reporter.panicOnBuild( - extendErrorIdWithPluginName(pluginName, errorMeta), + extendErrorIdWithPluginName(pluginName, { ...errorMeta, pluginName }), error ) } From 6dd609e7cdd68e8d59a2486e79eab950152231bf Mon Sep 17 00:00:00 2001 From: Sidhartha Chatterjee Date: Fri, 6 Nov 2020 20:10:09 +0530 Subject: [PATCH 2/3] Add plugin name for unstructured errors too --- .../src/structured-errors/error-map.ts | 7 +-- packages/gatsby/src/utils/api-runner-node.js | 51 ++++++++++++++++--- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/packages/gatsby-cli/src/structured-errors/error-map.ts b/packages/gatsby-cli/src/structured-errors/error-map.ts index 3648ebfe87714..bde43bf84a0d9 100644 --- a/packages/gatsby-cli/src/structured-errors/error-map.ts +++ b/packages/gatsby-cli/src/structured-errors/error-map.ts @@ -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, diff --git a/packages/gatsby/src/utils/api-runner-node.js b/packages/gatsby/src/utils/api-runner-node.js index 30a97b766fcf4..7b19667e4c7df 100644 --- a/packages/gatsby/src/utils/api-runner-node.js +++ b/packages/gatsby/src/utils/api-runner-node.js @@ -169,23 +169,62 @@ function extendLocalReporterToCatchPluginErrors({ setErrorMap = errorMap => reporter.setErrorMap(getErrorMapWithPluginName(pluginName, errorMap)) - error = (errorMeta, error) => + error = (errorMeta, error) => { + const errorMetaWithPluginName = + typeof errorMeta === `string` + ? { + context: { + sourceMessage: errorMeta, + }, + pluginName, + } + : { + ...errorMeta, + pluginName, + } reporter.error( - extendErrorIdWithPluginName(pluginName, { ...errorMeta, pluginName }), + extendErrorIdWithPluginName(pluginName, errorMetaWithPluginName), error ) + } - panic = (errorMeta, error) => + panic = (errorMeta, error) => { + const errorMetaWithPluginName = + typeof errorMeta === `string` + ? { + context: { + sourceMessage: errorMeta, + }, + pluginName, + } + : { + ...errorMeta, + pluginName, + } reporter.panic( - extendErrorIdWithPluginName(pluginName, { ...errorMeta, pluginName }), + extendErrorIdWithPluginName(pluginName, errorMetaWithPluginName), error ) + } - panicOnBuild = (errorMeta, error) => + panicOnBuild = (errorMeta, error) => { + const errorMetaWithPluginName = + typeof errorMeta === `string` + ? { + context: { + sourceMessage: errorMeta, + }, + pluginName, + } + : { + ...errorMeta, + pluginName, + } reporter.panicOnBuild( - extendErrorIdWithPluginName(pluginName, { ...errorMeta, pluginName }), + extendErrorIdWithPluginName(pluginName, errorMetaWithPluginName), error ) + } } return { From 086c6c9b8758626fe0022837a27fb9a9fcb82621 Mon Sep 17 00:00:00 2001 From: Sidhartha Chatterjee Date: Fri, 6 Nov 2020 20:18:40 +0530 Subject: [PATCH 3/3] Refactoring --- packages/gatsby/src/utils/api-runner-node.js | 61 ++++++++------------ 1 file changed, 25 insertions(+), 36 deletions(-) diff --git a/packages/gatsby/src/utils/api-runner-node.js b/packages/gatsby/src/utils/api-runner-node.js index 7b19667e4c7df..e8c43fa5bb0c4 100644 --- a/packages/gatsby/src/utils/api-runner-node.js +++ b/packages/gatsby/src/utils/api-runner-node.js @@ -165,23 +165,28 @@ 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(getErrorMapWithPluginName(pluginName, errorMap)) error = (errorMeta, error) => { - const errorMetaWithPluginName = - typeof errorMeta === `string` - ? { - context: { - sourceMessage: errorMeta, - }, - pluginName, - } - : { - ...errorMeta, - pluginName, - } + const errorMetaWithPluginName = addPluginNameToErrorMeta( + errorMeta, + pluginName + ) reporter.error( extendErrorIdWithPluginName(pluginName, errorMetaWithPluginName), error @@ -189,18 +194,10 @@ function extendLocalReporterToCatchPluginErrors({ } panic = (errorMeta, error) => { - const errorMetaWithPluginName = - typeof errorMeta === `string` - ? { - context: { - sourceMessage: errorMeta, - }, - pluginName, - } - : { - ...errorMeta, - pluginName, - } + const errorMetaWithPluginName = addPluginNameToErrorMeta( + errorMeta, + pluginName + ) reporter.panic( extendErrorIdWithPluginName(pluginName, errorMetaWithPluginName), error @@ -208,18 +205,10 @@ function extendLocalReporterToCatchPluginErrors({ } panicOnBuild = (errorMeta, error) => { - const errorMetaWithPluginName = - typeof errorMeta === `string` - ? { - context: { - sourceMessage: errorMeta, - }, - pluginName, - } - : { - ...errorMeta, - pluginName, - } + const errorMetaWithPluginName = addPluginNameToErrorMeta( + errorMeta, + pluginName + ) reporter.panicOnBuild( extendErrorIdWithPluginName(pluginName, errorMetaWithPluginName), error