Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc - error event vs. error callback #1779

Closed
tomruggs opened this issue Sep 21, 2015 · 3 comments
Closed

Doc - error event vs. error callback #1779

tomruggs opened this issue Sep 21, 2015 · 3 comments
Labels

Comments

@tomruggs
Copy link

The doc does not mention the difference between the error event and callback on error. It would be nice if there was a blurb about how the two differ and when the callback would be called but no event would be emitted. For instance if the URL or options used contain an invalid entry.

For a bad URL you get both en error event and a callback with an error value:

request('http://0.0.0.0/asdf', function(error, response) {
  console.log('error callback');
}).on('error', function(error) {
  console.log('error event');
})

Result:
error callback
error event

But for a malformed only the callback fires:

request('i like pizza', function(error, response) {
  console.log('error callback');
}).on('error', function(error) {
  console.log('error event');
});

Result:
error callback
@simov
Copy link
Member

simov commented Sep 23, 2015

Hi, @tomruggs, probably this thread might help you: #1558

@mnebuerquo
Copy link

I agree this is confusing. I think the documentation needs to be updated to clearly show that the callback is required even when handling the event.

From the docs:

To easily handle errors when streaming requests, listen to the error event before piping:

request
  .get('http://mysite.com/doodle.png')
  .on('error', function(err) {
    console.log(err)
  })
  .pipe(fs.createWriteStream('doodle.png'))

This does not tell us that we need to always have a callback to handle certain errors.

Maybe write it this way instead:

To easily handle errors when streaming requests, listen to the error event before piping.
Warning: You must also include a callback to catch certain errors which occur during initialization. These will not emit an event, so the callback is the only way to handle them.

request
  .get('http://mysite.com/doodle.png',function(err){
    if(err){ console.log(err); }
  })
  .on('error', function(err) {
    console.log(err)
  })
  .pipe(fs.createWriteStream('doodle.png'))

(To those of you who get notifications on this thread, I accidentally posted this while logged in with the wrong account, so it is deleted and reposted. Sorry about the double notification.)

@stale
Copy link

stale bot commented Nov 23, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Nov 23, 2018
@stale stale bot closed this as completed Nov 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants