Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I catch zlib errors in node.js

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?

like image 229
Joshua Smith Avatar asked Sep 26 '22 08:09

Joshua Smith


1 Answers

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

like image 89
Oles Savluk Avatar answered Sep 28 '22 07:09

Oles Savluk