#1772·superagent

[fix] Can't handle socket hangup (pipe)

Author: KelbradCreated Jul 23, 2023Updated Mar 27, 2026
LabelsBug

Describe the bug

Node.js version: v18.17.0

OS version: Windows (but confirmed on MacOS as well)

Description:

Hi there,

I dont see any way of handling socket errors after initial connection has been established. I'm trying to download a large file with superagent and during download sometimes something happens and connection is broken. I tried to catch that error but it doesn't seem that superagent is notifing about those errors at all.

See code sample:

typescript
public static downloadFileToStream(url: string, stream: WriteStream) {
        return new Promise((resolve, reject) => {
            superagent.get(url)
                .on('error', (e) => {
                    logger.error('Req Error!', e);
                    reject(e);
                })
                .pipe(stream)
                .on('error', (e) => {
                    logger.trace('Error!', e);
                    reject(e);
                })
                .on('close', () => {
                    logger.trace('File downloaded!');
                    resolve({});
                })
        });
    }

Actual behavior

If error occurs during connection establishing, is see proper Req Error log messages, but after that, if error occurs during actual data transfer, I dont see anything.

Expected behavior

I would expect to get error event, when error occurs during data transfer. Afaik this problem may be just related to stream operation (pipe), I havent verified that without using streams

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.
  • I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.