From b4d8342008de8fac199e3a524130573ba81ed354 Mon Sep 17 00:00:00 2001
From: Lennart <lekoarts@gmail.com>
Date: Tue, 20 Oct 2020 11:16:41 +0200
Subject: [PATCH] fix(gatsby): Handle double prefix case for
 `extendErrorIdWithPluginName` (#27547)

Co-authored-by: Peter van der Zee <209817+pvdz@users.noreply.github.com>
---
 .../__tests__/plugin-errors.js                | 19 ++++++++++++++++++-
 .../structured-plugin-errors/gatsby-node.js   |  7 +++++++
 packages/gatsby/src/utils/api-runner-node.js  |  7 ++++---
 3 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/integration-tests/structured-logging/__tests__/plugin-errors.js b/integration-tests/structured-logging/__tests__/plugin-errors.js
index 1d34960c29b96..84f4f4b28d767 100644
--- a/integration-tests/structured-logging/__tests__/plugin-errors.js
+++ b/integration-tests/structured-logging/__tests__/plugin-errors.js
@@ -59,7 +59,24 @@ describe(`Plugin Errors`, () => {
           action: expect.objectContaining({
             type: `LOG`,
             payload: expect.objectContaining({
-              level: "ERROR",
+              level: `ERROR`,
+              category: `SYSTEM`,
+              text: `Error text is MORE ERROR!`,
+              code: `structured-plugin-errors_12345`
+            })
+          })
+        })
+      ])
+    )
+
+    expect(events).toEqual(
+      expect.arrayContaining([
+        expect.objectContaining({
+          type: `LOG_ACTION`,
+          action: expect.objectContaining({
+            type: `LOG`,
+            payload: expect.objectContaining({
+              level: `ERROR`,
               category: `SYSTEM`,
               text: `Error text is PANIC!`,
               code: `structured-plugin-errors_1337`,
diff --git a/integration-tests/structured-logging/plugins/structured-plugin-errors/gatsby-node.js b/integration-tests/structured-logging/plugins/structured-plugin-errors/gatsby-node.js
index 3b8a82c6a1eca..8edb2b4d76438 100644
--- a/integration-tests/structured-logging/plugins/structured-plugin-errors/gatsby-node.js
+++ b/integration-tests/structured-logging/plugins/structured-plugin-errors/gatsby-node.js
@@ -6,11 +6,18 @@ exports.onPreInit = ({ reporter }) => {
       category: "SYSTEM",
       docsUrl: `https://www.gatsbyjs.org/docs/gatsby-cli/#new`,
     },
+    "12345": {
+      text: context => `Error text is ${context && context.someProp}`,
+      level: "ERROR",
+      category: "SYSTEM",
+      docsUrl: `https://www.gatsbyjs.com/docs/cheat-sheet/`,
+    }
   })
 
   reporter.info("setErrorMap")
 
   if (process.env.PANIC_IN_PLUGIN) {
+    reporter.error({ id: "structured-plugin-errors_12345", context: { someProp: `MORE ERROR!` } })
     reporter.panic({ id: "1337", context: { someProp: `PANIC!` } })
   }
 }
diff --git a/packages/gatsby/src/utils/api-runner-node.js b/packages/gatsby/src/utils/api-runner-node.js
index b4ea0c5e95b02..d0116580acf31 100644
--- a/packages/gatsby/src/utils/api-runner-node.js
+++ b/packages/gatsby/src/utils/api-runner-node.js
@@ -130,9 +130,10 @@ function getLocalReporter({ activity, reporter }) {
 }
 
 function extendErrorIdWithPluginName(pluginName, errorMeta) {
-  if (typeof errorMeta === `object`) {
-    const id = errorMeta && errorMeta[`id`]
-    if (id) {
+  const id = errorMeta?.id
+  if (id) {
+    const isPrefixed = id.includes(`${pluginName}_`)
+    if (!isPrefixed) {
       return {
         ...errorMeta,
         id: `${pluginName}_${id}`,