olavea / Lillian-and-Ola-Vea-com

Lillian-and-Ola-Vea-com
1 stars 0 forks source link

4. Custom error map needs to be initiated in onPluginInit #29

Open olavea opened 2 years ago

olavea commented 2 years ago
  1. Custom error map needs to be initiated in onPluginInit

Got an error:

ReferenceError: CODES is not defined

// gatsby-node.js
// Cap Ola Vea adds Gatsby's structured logs
const ERROR_MAP = {
  [CODES.Generic]: {
    text: (context) => context.sourceMessage,
    level: `ERROR`,
    type: `PLUGIN`,
  },
  [CODES.MissingResource]: {
    text: (context) => context.sourceMessage,
    level: `ERROR`,
    type: `PLUGIN`,
    category: `USER`,
  },
};
const initializePlugin = ({ reporter }) => {
  if (reporter.setErrorMap) {
    reporter.setErrorMap(ERROR_MAP);
  }
};

// need to conditionally export otherwise it throws an error for older versions
if (coreSupportsOnPluginInit === "stable") {
  exports.onPluginInit = initializePlugin;
} else if (coreSupportsOnPluginInit === "unstable") {
  exports.unstable_onPluginInit = initializePlugin;
} else {
  exports.onPreInit = initializePlugin;
}

depend on 🦁

raae commented 2 years ago

Would need to define CODES, but we don't really need this I think. But could be interesting for me to look into for better plugin error handling.