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.

Error handling - no output from .on('error')

See original GitHub issue

I would like to plugin my custom error handling/notifier. In my other recipes such as sass, I simply use:

.on('error', (error) => {
    this.notifyError(error, watching)
})

I see this plugin uses throw Error instead of a stream.emit('error') similar to scssLint

In order to work around this issue where #failAfterError doesn’t emit an error to my handler, I implemented a very ugly #result handler mostly duplicating #failAfterError but it will trigger my own notifier (#results fails to notify).

Note: this custom error handling attempts led me to discover to #134

I’m new to gulp, but it seems like I should be able to listen for an error based on patterns of use from other plugins. Is there any recommendation to improve this code? It seems like an unnecessary hack.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
adametrycommented, Feb 4, 2016

Note: I’ll get into a bit of detail here. So, if you just want a solution, skip to the workarounds.

Would I be right to assume that you’re referring to the throw in failOnError or failAfterError? If so, this takes advantage of a convenience provided the result and results methods (respectively). Both use the tryResultAction method to catch thrown errors and pass them to the stream (the stream’s onwrite function emits the error).

I believe some confusion may be when the error is thrown. The failAfterError method creates a results (plural) stream, which waits until it has ended (technically, the “prefinish” event) to perform it’s action once (collectively). Alternatively, the failOnError method creates a result (singular) stream, which will act (immediately) on each chunk/file. Likewise, the format method uses results, and the formatEach method uses result.

In your code, you work around the issue of failAfterError not behaving as expected by creating a result stream and use logic from the failAfterError method. In this case, you could simply use failOnError for the same effect; however, is likely end the stream before format can print lint results. You can simply use failAfterError in place of the custom result stream to emit an error after linting & printing all files.

Now, to the heart of the matter: no “error” event. It appears that the issue may be that gulp-if is muting the error emitted from failAfterError. As you demonstrate in your code, throwing in the result method results in an “error” event.

Three simple workarounds:

  1. Set the “error” event listener on the failAfterError stream (rather than the stream that gulpIf returns)
  2. Remove gulpIf, use results instead of result, and add !watching to its failure condition.
  3. Remove gulpIf and pipe to failAfterError with the “error” listener only if !watching (be sure to return the piped stream).

On a side note, you can throw anything from a result or results method. gulp-eslint will wrap handle wrapping the thrown value in a PluginError.

0reactions
rosskevincommented, Mar 12, 2016

This is a problem somewhere with gulp v3, I’m switching to gulp v4 and everything is working as expected, very strange indeed, but I’m going to close this as v4 is close to release and this probably isn’t worth tracking down.

Read more comments on GitHub >

github_iconTop Results From Across the Web

30. Errors and Exception Handling | Python Tutorial
It's a loop, which breaks only if a valid integer has been given. The while loop is entered. The code within the try...
Read more >
On Error statement (VBA) | Microsoft Learn
If you don't use an On Error statement, any run-time error that occurs is fatal; that is, an error message is displayed and...
Read more >
Error Handling - VBScript in a Nutshell [Book] - O'Reilly
Error handling does not involve finding errors in your scripts. ... The first is the On Error statement, which informs the VBScript engine...
Read more >
VBScript -- Using error handling - Stack Overflow
Since facade() doesn't handle errors (there is no On error resume next in facade() ), the error will be returned to main() and...
Read more >
VBA Error Handling - A Complete Guide - Excel Macro Mastery
On Error GoTo 0 – the code stops at the line with the error and displays a message. On Error Resume Next –...
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