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

Body stream error is not caught by Got #1046

Closed
2 tasks done
ohana54 opened this issue Feb 3, 2020 · 9 comments · Fixed by #1054
Closed
2 tasks done

Body stream error is not caught by Got #1046

ohana54 opened this issue Feb 3, 2020 · 9 comments · Fixed by #1054
Labels
bug Something does not work as it should

Comments

@ohana54
Copy link

ohana54 commented Feb 3, 2020

Describe the bug

  • Node.js version: 12.14.1
  • OS & version: MacOS 10.13.4

When using streams to upload a file, an error thrown from the stream read (for example, passing an inexistent file path) is not being caught by got.

This looks similar to #614

Actual behavior

The code in the example throws an error that is not caught by got itself.
Console output:

events.js:200
      throw er; // Unhandled 'error' event
      ^

Error: ENOENT: no such file or directory, open './no-file.txt'
Emitted 'error' event on ReadStream instance at:
    at internal/fs/streams.js:120:12
    at FSReqCallback.oncomplete (fs.js:146:23) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: './no-file.txt'
}

Expected behavior

The error should be caught and "caught error" should be logged to the console.

Code to reproduce

const got = require('got')
const fs = require('fs')

got
  .put('http://a.com/', {
    body: fs.createReadStream('./file-that-does-not-exist.txt'),
  })
  .catch(() => console.log('caught error'))

PR with failing test: #1047

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.
@szmarczak
Copy link
Collaborator

That's because we use stream.pipeline and it causes all streams to throw:

  • the Got stream itself,
  • the body.

I wouldn't consider this a bug, although it's quite annoying behavior.

@sindresorhus Should this be configurable through an option? For example catchBodyErrors.

@szmarczak szmarczak changed the title Error from stream is not being caught Body stream error is not caught by Got Feb 6, 2020
@szmarczak szmarczak added the question The issue is a question about Got label Feb 6, 2020
@ohana54
Copy link
Author

ohana54 commented Feb 6, 2020

@szmarczak I agree it can be considered as not a bug (I had second thoughts when I opened the issue), although it's [possibly naively] expected that the library will catch any related exceptions, like it already does very nicely.
Also, handling it on the user side is rather ugly :)

Thank you for considering a solution for this, I really appreciate it.

@sindresorhus
Copy link
Owner

Got is the one consuming the fs.createReadStream('./file-that-does-not-exist.txt') stream. It's Got's responsibility to catch the body error and forward it to the promise. I don't think we need an option for that.

@sindresorhus
Copy link
Owner

That's because we use stream.pipeline and it causes all streams to throw:

We should open an issue on Node.js about this. That's definitely not the wanted (or useful) behavior. I assumed it would forward the error to the last stream or the callback to stream.pipeline if there's one.

@szmarczak szmarczak added bug Something does not work as it should and removed question The issue is a question about Got labels Feb 6, 2020
@szmarczak
Copy link
Collaborator

I assumed it would forward the error to the last stream or the callback to stream.pipeline if there's one.

You assumed correctly. stream.pipeline works just fine. I just misguessed it :'(

It's just that there's no error handler for options.body before attaching it to pipeline. Will send a fix ASAP.

@szmarczak szmarczak mentioned this issue Feb 6, 2020
18 tasks
@szmarczak
Copy link
Collaborator

It's just that it throws the error before Got has the time to attach a handler...

@szmarczak
Copy link
Collaborator

Fixed :)

@ohana54
Copy link
Author

ohana54 commented Feb 6, 2020

Awesome, thanks!!

@sindresorhus
Copy link
Owner

And released: https://github.com/sindresorhus/got/releases/tag/v10.5.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something does not work as it should
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants