Error handling with .pipe()? #87

Closed
tmcw opened this Issue Oct 12, 2011 · 3 comments

Comments

Projects
None yet
3 participants

tmcw commented Oct 12, 2011

I'm attempting to refactor some code that currently uses node-get:

        (new get(url)).toDisk(dl, function(err, file) {
            pool.release(obj);
            if (err) {
                downloads[url].emit('done', err);
                delete downloads[url];
                return callback(err);
            }
            fs.rename(dl, filepath, function(err) {
                downloads[url].emit('done', err, filepath);
                delete downloads[url];
                return callback(err, filepath);
            });
        });

Into request, and having some trouble. My current 'blue sky code' is

        var req_dl = request(url).on('error', function(err) {
            pool.release(obj);
            downloads[url].emit('done', err);
            delete downloads[url];
            return callback(err);
        })
        .on('end', function() {
            pool.release(obj);
            fs.rename(dl, filepath, function(err) {
                downloads[url].emit('done', err, filepath);
                delete downloads[url];
                return callback(err, filepath);
            });
        })
        .pipe(fs.createWriteStream(dl));

Which fails, because there is no eventemitter to bind the .on()s on. It seems like there are two ways to use request: simple chaining with .pipe() and providing a callback. Earlier I was doing something else: allowing one to stream the file but also catch errors. I don't see an exposed way to both use .pipe() and grab errors or success from request. Is there one, that just isn't documented or obvious? Thanks!

I just tried this (the 'blue sky code' above, minus the undefined vars) and it worked as expected. request() returns a new Request, which inherits from Stream. Perhaps you have an old version, or something?

tmcw commented Oct 26, 2011

@mfncooper: it works as expected when you don't have an error, but my problem deals with catching errors when the URL is bad or server is down.

Owner

mikeal commented Feb 18, 2012

url is entire bad should throw, server is down should emit. this should be fixed in node 0.6, in 0.4 there were several socket errors we couldn't trap.

@mikeal mikeal closed this Feb 18, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment