xDimGG / node-steamapi

An object-oriented Steam API wrapper for Node.js developers.
https://www.npmjs.com/package/steamapi
177 stars 43 forks source link

ETIMEDOUT not handeled error #11

Closed luja96 closed 5 years ago

luja96 commented 5 years ago

In fetch.js file I think there is a mistype: op.once('errror', reject); should be op.once('error', reject); (There is an extra 'r' in 'error'). But thats not the main issue I want to address here.

After performing few thousand of request occasionally I get:

Error: connect ETIMEDOUT 92.122.156.90:443 at Object._errnoException (util.js:992:11) at _exceptionWithHostPort (util.js:1014:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14) I have fixed this by catching the error after fetch in fetch.js

return new Promise((resolve, reject) => {
        fetch(options, res => {
            const ce = res.headers['content-encoding'];
            let data = '';
            let op = res;
            if (ce === 'deflate') res.pipe(op = createDeflate());
            if (ce === 'gzip') res.pipe(op = createGunzip());

            op.on('data', chunk => data += chunk);
            op.on('error', reject);
            op.once('end', () => {
                if (res.statusCode === 500) return reject(new Error('Internal server error'));
                if (res.statusCode === 403) return reject(new Error('Invalid key'));
                if (res.statusCode === 400) return reject(new Error(reg.test(data) ? data.match(reg)[1] : data));
                if (res.statusCode !== 200) return reject(new Error(res.statusMessage));
                try {
                    resolve(JSON.parse(data));
                } catch (_) {
                    reject(new Error(data));
                }
            });
        }).on("error", function(error){
            console.log(error);
        });
    });
xDimGG commented 5 years ago

Should be fixed in https://github.com/xDimGG/node-steamapi/commit/11df83864821b573e0b375338cd1e316ad80707c.