Skip to content

Commit

Permalink
[fix] effectively handle errors when we hit particular statusCodes as…
Browse files Browse the repository at this point in the history
… well
  • Loading branch information
jcrugzz committed Mar 6, 2014
1 parent 8640090 commit 337f1e5
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions node.js/lib/client/client.js
Expand Up @@ -232,9 +232,7 @@ Client.prototype.request = function (options, callback) {
error.statusCode = statusCode;
error.result = result;
}
//
// If we don't pass a callback here, don't try and do anything fancy
//
// Only call this if we pass a callback
if (callback) {
// Only add the response argument when people ask for it
if (callback.length === 3) return callback(error, result, res);
Expand Down Expand Up @@ -281,7 +279,30 @@ Client.prototype.upload = function (options, callback) {
if (!req) return;

req.on('error', callback);
req.on('response', callback.bind(null, null));
req.on('response', function (res) {
//
// TODO: clean this up. This is an extraneous case that offsets the main
// use case of the api so we repeat some code here
//
if (failCodes[res.statusCode]) {
var error = new Error('Nodejitsu Error (' + statusCode + '): ' + failCodes[statusCode]);
error.statusCode = statusCode;
error.result = '';

res.on('data', function (d) {
error.result += d;
});

res.on('end', function () {
try { error.result = JSON.parse(error.result) }
catch (ex) {}
callback(error);
});
return;
}

callback(null, res);
});

// Notify that we have started the upload procedure and give it a reference
// to the stat.
Expand Down

0 comments on commit 337f1e5

Please sign in to comment.