5

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?

Joshua Smith
  • 3,689
  • 4
  • 32
  • 45
  • 1
    These are asynchronous functions. They return right away. The exception happens later. There is nothing to wrap a try/catch around. – Joshua Smith Feb 16 '16 at 18:22

1 Answers1

5

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

Community
  • 1
  • 1
Oles Savluk
  • 4,315
  • 1
  • 26
  • 40
  • Sweet! I will try that. Unfortunately, it's hard to reproduce. I'll come back and give you the √ if I stop seeing the exception. :) – Joshua Smith Feb 16 '16 at 18:26
  • I emulated it by removing write access to a file it was downloading, so there would be a low-level IO error, and this caught it, so I'm accepting the answer. Thank you so much! – Joshua Smith Feb 16 '16 at 18:49
  • @JoshuaSmith I have edited answer, because I was catching errors related to writing the file, but ignoring errors related to decompressing. Now answer is correct. – Oles Savluk Feb 16 '16 at 19:05