Cancel a build whenever an error is thrown?

Is it possible to cancel a build whenever an error is thrown? Or, how can I programmatically cancel a build during from within my code?

Today we had several important pages of our website go down because of a Type Error being thrown during rendering. Ideally, we’d like any errors to cause the build to fail so that it triggers an email alert for us to investigate.

@ZachD Builds already do fail when they encounter an error.

In fact, the default behavior is that they will fail even on a warning.

This is for the code that executes during the build though, and isn’t the case if you’re utilising something that is dynamically generating content after the build.

E.g. If you’re using a system that is rendering pages with functions, and the function code contains an error, it would fail when executed at runtime.

If you really are talking about the build process itself, you can cause your build to fail by simply returning any non-zero exit code.

So if you were working with nodejs you could use process.exit(1)

2 Likes