I'm downloading content from a server that might be compressed, so I'm using this boilerplate I found in various places:
var file = fs.createWriteStream(filename);
var request = https.get(url, function(response) {
switch (response.headers['content-encoding']) {
case 'gzip':
response.pipe(zlib.createGunzip()).pipe(file);
break;
case 'deflate':
response.pipe(zlib.createInflate()).pipe(file);
break;
default:
response.pipe(file);
break;
}
file.on('finish', function() {
console.log("Done: "+url);
file.close(function() {});
});
}).on('error', function(err) { // Handle errors
fs.unlink(filename, function() {});
console.log(err+" "+url);
});
Trouble is, if the HTTPS request fails from a network error, I sometimes get this exception thrown:
Error: unexpected end of file
at Zlib._handle.onerror (zlib.js:363:17)
The exception isn't caught by that "error" event handler. So how do I catch it, so I can clean up the file properly and know to try again?
You have handler for errors related to request, but not for errors related to decompressing and saving the file. Try catching errors in decompressed stream:
var decompressed;
switch (response.headers['content-encoding']) {
case 'gzip':
decompressed = response.pipe(zlib.createGunzip());
break;
case 'deflate':
decompressed = response.pipe(zlib.createInflate());
break;
default:
decompressed = response;
break;
}
decompressed.on('error', function(err) {
// handle error
});
decompressed.pipe(file);
For more reading follow this answer - Error handling with node.js streams
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With