Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixing callback ordering and error handling. Needs another look till …

…merge
  • Loading branch information...
commit 60c48128adfb4315f30b07738c5d46bd96a3105c 1 parent 3a6a110
@tmcw tmcw authored
Showing with 9 additions and 6 deletions.
  1. +9 −6 lib/node-get/node-get.js
View
15 lib/node-get/node-get.js
@@ -71,7 +71,7 @@ Get.prototype.request = function(callback) {
if (!(this.uri_o.protocol &&
(this.uri_o.protocol == 'http:' || this.uri_o.protocol == 'https:') &&
this.uri_o.hostname)) {
- return callback.call(this, new Error('Invalid URL'));
+ return callback.call(this, null, new Error('Invalid URL'));
}
// TODO: should pronode-getxies support HTTPS?
@@ -112,7 +112,9 @@ Get.prototype.perform = function(callback, times) {
times = times || 1;
- var clientrequest = this.request(function(response) {
+ // Reversed form of the callback because node's api is weird
+ var clientrequest = this.request(function handleResponse(response, err) {
+ if (err) return callback.call(this, err, null);
if (response.statusCode >= 300 &&
response.statusCode < 400 &&
response.headers.location) {
@@ -131,10 +133,10 @@ Get.prototype.perform = function(callback, times) {
// failure
var err = new Error('Server returned HTTP ' + response.statusCode);
err.status = response.statusCode;
- callback.call(this, err, response);
+ return callback.call(this, err, response);
} else {
// success
- callback.call(this, null, response);
+ return callback.call(this, null, response);
}
}.bind(this));
@@ -142,7 +144,7 @@ Get.prototype.perform = function(callback, times) {
if (clientrequest) {
// Handle DNS-level errors, like ECONNREFUSED
clientrequest.on('error', function(err) {
- callback.call(this, err);
+ return callback.call(this, err);
}.bind(this));
// TODO: fix when/if gzip is supported.
@@ -191,9 +193,10 @@ Get.prototype.toDisk = function(filename, callback) {
// - @param {Function} callback.
Get.prototype.asString = function(callback) {
// TODO: catch all errors
- this.perform(function(err, response) {
+ this.perform(function reponseToString(err, response) {
if (err) return callback(err);
+ if (!response.headers) console.log(response);
switch ((response.headers['content-type'] || '').toLowerCase()) {
case 'binary/octet-stream':
case 'application/octet-stream':
Please sign in to comment.
Something went wrong with that request. Please try again.