Intentionally fail a build process from code

We currently have an axios call that talks to our CMS, in order to provide content for our nuxt pages.

There’s a rare scenario that for whatever reason, the axios call fails (CMS unavailable/rate limited, whatever).

In these instances, is there a way to ensure the build process in Netlify doesn’t go ahead?

We don’t want to publish a site that has no content. The current scenario means we end up publishing a site with no pages, and everything’s a 404 because the build process couldn’t talk to the cms.

Is there some kind of error we can throw that will stop the netlify build process?

Hi, @belugaed. Any process that exits will a non-zero exit code will be treated as a failure to build correctly and prevent the build from being deployed.

With Node.js, you can set the exit code with this:

process.exitCode = 1

You should be able to set anywhere in your code when you want a build to fail. When the normal process exit occurs, it should use that exit code and, because it is non-zero, the build will not be deployed. The build will be cancelled due to the error exit code (as all non-zero exit codes are considered errors on Linux systems).

If that doesn’t work (for example, because you are not using Node.js) or if there are other questions about this, please reply here anytime.

If you want to immediately terminate the process with a non-zero exit code you can use process.exit(1)

See:
https://nodejs.org/api/process.html#processexitcode

Thanks @nathanmartin @luke

We’re using nuxt - but looks like we can pass a flag through that does the same thing to pass back up to the node process. Will give it a go, thanks!

@belugaed Great!

process.exit(1) is worth knowing purely because it isn’t framework specific.

You can use it with Nuxt in a pinch, or any random node script.

For example if you had a script you were executing prior to your Nuxt build, where you wanted to ensure it never progressed to the Nuxt build at all, under some specific circumstance.

1 Like