ladjs / superagent

Ajax for Node.js and browsers (JS HTTP client). Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.
https://ladjs.github.io/superagent/
MIT License
16.57k stars 1.33k forks source link

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

Open Kelbrad opened 11 months ago

Kelbrad commented 11 months ago

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:

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