Skip to content

Streaming request body errors are swallowed #753

Description

@achingbrain

I'm not sure if I'm looking at this wrong, but if I specify a stream as the request body, and that stream errors, I can't see how to receive the error from the request body stream.

Take a look at the following test:

const { PassThrough } = require('stream')

it('should propagate errors from streaming bodies', async () => {
  const body = new PassThrough()
  const err = new Error('Aargh!')

  const req = fetch('https://example.com', {
    method: 'POST',
    body
  })

  // the request body failed, for whatever reason
  body.emit(err)

  // have to end the stream after the error otherwise `await req` below does not resolve
  body.end()

  try {
    // should something here throw with the request body error?
    const res = await req
    await res.text()

    throw new Error('What happened to err?')
  } catch (e) {
    expect(e.message).to.equal(err.message)
  }
})
  1) should propagate errors from streaming bodies:

      AssertionError: expected 'What happened to err?' to equal 'Aargh!'
      + expected - actual

      -What happened to err?
      +Aargh!

The await res.text() returns the response from the server as if nothing went wrong when actually the request body was truncated.

At the moment it looks like I need to set up the error listener on the request body stream externally, along with an AbortController and kill the request manually if the request body errors which adds a whole load of boilerplate to my codebase.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions