queen.raae.codes · a developer’s logbook
1 min read

Stop the build with `reporter.panic`

Written in 2022, 4 years ago. Tools and advice may have moved on since.

On last week’s show, we sourced data from Supabase in the form of little avatars.

If the sourcing fails though, we’ll get problems down the road with the Avatars component:

Cannot query field "allUserAvatar" on type "Query"

It would be better to fail as fast as possible, and you may do so with reporter.panic.

// File: gatsby-node.js

const { createClient } = require("@supabase/supabase-js");

const supabaseUrl = process.env.GATSBY_PUBLIC_SUPABASE_URL;
const supabaseServiceKey = process.env.SUPABASE_SERVICE_KEY;

const serviceSupabase = createClient(supabaseUrl, supabaseServiceKey);

exports.sourceNodes = async (gatsbyUtils) => {
  const { actions, createNodeId, createContentDigest, reporter } = gatsbyUtils;
  const { createNode } = actions;

  const { data, error } = await serviceSupabase
    .from("avatars")
    .select("username, avatar_url")
    .limit(20);

  if (error) {
    // 👇👇👇 Stops the build
    reporter.panic("No avatars sourced", error);
  }

  data.forEach((item) => {
    createNode({
      id: createNodeId(item.username),
      avatarUrl: item.avatar_url,
      username: item.username,
      internal: {
        type: "UserAvatar",
        contentDigest: createContentDigest(item),
      },
    });
  });
};

Read more about the reporter helper in the Gatsby Docs.

 
All the best,
Queen Raae

Join the crew!

All the shenanigans from me and my piratical family, straight to your inbox.

Search the log

↑↓ to move · Enter to open · Esc to close All entries →