Skip to content

Doesn't catch body (Stream) errors #887

@builtbylane

Description

@builtbylane

How do I prevent my app from crashing when posting a stream to an offline destination? In both examples below I get:

internal/streams/legacy.js:59
      throw er; // Unhandled stream error in pipe.
      ^

Error: connect ECONNREFUSED 127.0.0.1:2024
    at TCPConnectWrap.afterConnect

(I'm on node v12.1.0)

import FormData from 'form-data';
import got from 'got';
import fs from 'fs';

(async () => {
  try {
    const form = new FormData();
    form.append('upload', fs.createReadStream('filepath.jpg'));
    await got.post('http://offlinesite123.com', {
      body: form,
    });
  } catch (err) {
    console.error('does not catch', err);
  }
})();

this also crashes my app

try {
  const form = new FormData();
  form.append('upload', fs.createReadStream('filepath.jpg'));
  got.stream
    .post('http://offlinesite123.com', {
      body: form,
    })
    .on('error', err => console.error('this error gets reported but app still crashes'));
} catch (err) {
  console.error('does not catch', err);
}

Checklist

  • I have read the documentation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething does not work as it shouldexternalThe issue related to an external project

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions