mardi 31 mars 2015

Why is my stream error handler not being called?

I have the following restify handler:



var assert = require('assert-plus');
var request = require('request');

function postParseVideo(req, res, next) {
assert.string(req.body.videoSourceUrl, 'videoSourceUrl');

var stream = request.get({uri: req.body.videoSorceUrl});
stream.on('response', function(parseResponse) {
fnThatTakesAReadableStream(parseResponse, function(err, data) {
if (err) {
console.log(err);
next(err);
} else {
res.send(201, null, {Location: data.location});
next();
}
});
});
stream.on('error', function(err) {
console.log(err);
next(err);
});
};


When I run this, neither of the stream event handlers is ever called. Instead, an error bubbles up to the restify server: {"code":"InternalError","message":"options.uri is a required argument"}. I looked at the source for request v2.54.0, and it looks like that error message must be coming from line 406 of restify.js, which reads:



return self.emit('error', new Error('options.uri is a required argument'))


I have used streams successfully in a number of other languages, but I’m new to using them in JavaScript. It seems to me like request.get is throwing a synchronous error, when it should be emitting an 'error' event. Is there something I’m fundamentally misunderstanding about how streams are implemented in Node?


Aucun commentaire:

Enregistrer un commentaire