question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I throw an error from a plugin to prevent a build?

See original GitHub issue

❔ Question

How can I throw an error in a plugin that will prevent the build from continuing?

🔦 Context

I am developing a one-liner plugin whose only job is to run execSync('tsc --noEmit') before every build. It throws an error, and a warning is printed to the console, but the build continues on anyway. I can manually call process.exit(1) but that’ll kill the whole parcel process watching my files. Is it possible for a plugin to indicate that the build has failed?

💻 Code Sample

const { execSync } = require('child_process')
const path = require('path')

const cwd = process.cwd()
const binPath = execSync('yarn bin', { cwd, encoding: 'utf8' }).trim()
const tscCommand = `${path.join(binPath, 'tsc')} --noEmit`

module.exports = () => {
  // Will throw if tsc reports any issues
  execSync(tscCommand, { cwd, encoding: 'utf8', stdio: 'inherit' })
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
mischniccommented, Dec 1, 2019

This makes it on every rebuild (“before”/“when”), but you can’t abort a build.

module.exports = (bundler) => {
  bundler.on('buildStart', entryPoints => {
	  execSync(tscCommand, { cwd, encoding: 'utf8', stdio: 'inherit' })
  });
}
0reactions
mmiller42commented, Dec 1, 2019

buildStart worked perfectly, thanks!

If anyone happens to be looking for a full list of events, they’re all very neatly defined at https://github.com/parcel-bundler/parcel/blob/23ba2cc54f3286f60e2cab63eb358660c916139d/packages/core/types/index.js

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handle exceptions in plug-ins - Power Apps - Microsoft Learn
We recommend that you catch any error and throw an InvalidPluginExecutionException exception so you can control what is displayed to the user.
Read more >
How to prevent build failure with custom Maven Plugin?
If you want, you can catch all exceptions in the execute method and discard them. But is it really sensible to ignore everything...
Read more >
Dynamics 365 Raise Error in Plugin - Carl de Souza
To raise an error in the plugin code, we use InvalidPluginExecutionException. For example: throw new InvalidPluginExecutionException("Plugin ...
Read more >
Exception Best Practice | Softchief Learn
If you throw InvalidPluginExecutionException and do not provide a custom message, a generic default message is displayed in the error dialog ...
Read more >
Webpack client does not throw error code when build failed
I'd like to avoid to search for "ERROR" inside the client output to determine that the build failed. Thanks.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found