Is it possible to skip creating a page during build if that page ends up throwing during render?
This page is created programmatically via gatsby-node createPage
and it's possible that the data from a page query (from our CMS) is bad causing it to throw.
I don't want stop the build due to one bad page so ideally the page wouldn't be created or a fallback page would be put into its place (and an error event would be logged to Sentry or similar).
Any ideas on how to achieve this?
Edit: I didn't clarify my question enough so I wanted to add some context about what problem I'm trying to solve and why.
The error I'm trying to catch occurs during the render of a page during build time. This error occurs because the component I'm trying to render assumed something about the data that isn't true (but should be true).
For example, let's say I'm creating many pages for all the products on my site. The component expects that every product has imagesSizes
and calls imagesSizes.split(',')
during render. Because imagesSizes
is null
from the page query, the whole component throws an error and breaks the build.
Like @EliteRaceElephant has suggested, I have tried using React Error Boundaries and unfortunately, they don't work for SSR (which is used by Gatsby during build time). So even if I wrap my component in an error boundary, it still ends up breaking the build.
One a last note, the example I gave above is only one of the situations I run into where the data is bad and breaks the build.
What I'm trying to achieve is a simple fallback page for when any arbitrary error occurs during render during the build. My ideal solution would even allow me to throw
errors on purpose when certain assumptions I make about the data aren't true (because I'd rather send the user an error page instead of showing them a page with bad data).
Historically, when I've done SSR outside of Gatsby, I would simply wrap the whole call to ReactDOMServer.renderToString
in a try
catch
block and just return my fallback page in the catch
block.
What is the Gatsby equivalent of that?