Skip to content

Commit

Permalink
Merge pull request #426 from ryanseys/emit-error
Browse files Browse the repository at this point in the history
Emit errors from upstream on file upload
  • Loading branch information
stephenplusplus committed Mar 5, 2015
2 parents 381e535 + 869a466 commit 751d97f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,10 @@ File.prototype.startResumableUpload_ = function(stream, metadata) {
var writeStream = request(reqOpts);
writeStream.callback = util.noop;

writeStream.on('error', function(err) {
handleError(err);
});

writeStream.on('complete', function(res) {
util.handleResp(null, res, res.body, function(err, data) {
if (err) {
Expand Down Expand Up @@ -1123,7 +1127,6 @@ File.prototype.startResumableUpload_ = function(stream, metadata) {
}

stream.emit('error', err);
stream.end();
}
};

Expand Down
38 changes: 38 additions & 0 deletions test/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,44 @@ describe('File', function() {
writable.write('data');
});

it('should re-emit errors', function(done) {
var error = new Error('Error.');
var requestCount = 0;
file.bucket.storage.makeAuthorizedRequest_ = function(reqOpts, cb) {
requestCount++;

// respond to creation POST.
if (requestCount === 1) {
cb(null, null, { headers: { location: 'http://resume' }});
return;
}

// create an authorized request for the first PUT.
if (requestCount === 2) {
cb.onAuthorized(null, { headers: {} });
}
};

// respond to first upload PUT request.
request_Override = function() {
var stream = through();
setImmediate(function() {
stream.emit('error', error);
});
return stream;
};

var stream = duplexify();

stream
.on('error', function(err) {
assert.equal(err, error);
done();
});

file.startResumableUpload_(stream);
});

it('should start a simple upload if specified', function(done) {
var writable = file.createWriteStream({
metadata: METADATA,
Expand Down

0 comments on commit 751d97f

Please sign in to comment.