Permalink
Browse files

Refactor requests to better capture errors

Errors from HTTP, or failed statuses, will now be sent back to the callback. Additionally, the request-response handling code is now more centralized in the process_response() function, which optionally can add per-request error checking, but checks for the "typical" errors without any supervision.

So now, if the host is down, or the request fails, this can be messages appropriately to the user in a consistent fashion.
  • Loading branch information...
1 parent a1df7fb commit c6dd3fea604ad0f0c0671b8092f9e899005f26c2 @agnoster agnoster committed Feb 1, 2011
Showing with 42 additions and 30 deletions.
  1. +42 −30 nodester.js
View
@@ -17,10 +17,40 @@ var nodester = function (username, password, basehost) {
this.baseurl = "http://" + userbits + this.basehost + "/";
};
-nodester.prototype.coupon_request = function (email, cb) {
- request({uri: this.baseurl + "coupon", method: "POST", body: querystring.stringify({email: email}), headers: headers}, function (err, resp, body) {
- cb(err, JSON.parse(body));
+function process_response(cb, errfn) {
+ return function(err, response, body) {
+ var errCode = null;
+ if (!err && response && response.statusCode >= 400) {
+ errCode = response.statusCode;
+ }
+ var data = {};
+ try {
+ data = JSON.parse(body);
+ } catch (e) {
+ if (!err) err = { message: body };
+ data = body;
+ }
+ if (errfn) {
+ if (!err) err = errfn(data);
+ } else {
+ if (!err && data.status && data.status != 'success') err = { message: data.status }
+ }
+ if (errCode) {
+ if (err) { err.code = errCode; err.message = '[HTTP ' + errCode + '] ' + err.message; }
+ else err = { code: errCode, message: "HTTP Error " + errCode };
+ }
+ cb(err, data);
}
+}
+
+nodester.prototype.coupon_request = function (email, cb) {
+ request({uri: this.baseurl + "coupon", method: "POST", body: querystring.stringify({email: email}), headers: headers}, process_response(cb, function (data) {
+ if (!data.status) return data;
+ if (!/^success/.exec(data.status)) {
+ return { message: data.status };
+ }
+ return null;
+ })
);
};
@@ -41,35 +71,25 @@ nodester.prototype.user_create = function (user, pass, email, rsakey, coupon, cb
}),
headers: headers
},
- function (err, resp, body) {
- cb(err, JSON.parse(body))
- }
+ process_response(cb)
);
}
};
nodester.prototype.user_setpass = function (newpass, cb) {
- request({uri: this.baseurl + "user", method: 'PUT', body: querystring.stringify({password: newpass}), headers: headers}, function (err, response, body) {
- cb(err, JSON.parse(body));
- });
+ request({uri: this.baseurl + "user", method: 'PUT', body: querystring.stringify({password: newpass}), headers: headers}, process_response(cb));
};
nodester.prototype.user_setkey = function (rsakey, cb) {
- request({uri: this.baseurl + "user", method: 'PUT', body: querystring.stringify({rsakey: rsakey}), headers: headers}, function (err, response, body) {
- cb(err, JSON.parse(body));
- });
+ request({uri: this.baseurl + "user", method: 'PUT', body: querystring.stringify({rsakey: rsakey}), headers: headers}, process_response(cb));
};
nodester.prototype.apps_list = function (cb) {
- request({uri: this.baseurl + "apps", method: 'GET'}, function (err, response, body) {
- cb(err, JSON.parse(body));
- });
+ request({uri: this.baseurl + "apps", method: 'GET'}, process_response(cb));
};
nodester.prototype.app_create = function (name, start, cb) {
- request({uri: this.baseurl + "app", method: 'POST', body: querystring.stringify({appname: name, start: start}), headers: headers}, function (err, response, body) {
- cb(err, JSON.parse(body));
- });
+ request({uri: this.baseurl + "app", method: 'POST', body: querystring.stringify({appname: name, start: start}), headers: headers}, process_response(cb));
};
/*
@@ -79,9 +99,7 @@ nodester.prototype.app_set_start = function (name, start, cb) {
*/
nodester.prototype.app_running = function (name, running, cb) {
- request({uri: this.baseurl + "app", method: 'PUT', body: querystring.stringify({appname: name, running: running}), headers: headers}, function (err, response, body) {
- cb(err, JSON.parse(body));
- });
+ request({uri: this.baseurl + "app", method: 'PUT', body: querystring.stringify({appname: name, running: running}), headers: headers}, process_response(cb));
};
nodester.prototype.app_start = function (name, cb) {
@@ -97,21 +115,15 @@ nodester.prototype.app_stop = function (name, cb) {
};
nodester.prototype.app_delete = function (name, cb) {
- request({uri: this.baseurl + "app", method: 'DELETE', body: querystring.stringify({appname: name}), headers: headers}, function (err, response, body) {
- cb(err, JSON.parse(body));
- });
+ request({uri: this.baseurl + "app", method: 'DELETE', body: querystring.stringify({appname: name}), headers: headers}, process_response(cb));
};
nodester.prototype.app_info = function (name, cb) {
- request({uri: this.baseurl + "app/" + name, method: 'GET', headers: headers}, function (err, response, body) {
- cb(err, JSON.parse(body));
- });
+ request({uri: this.baseurl + "app/" + name, method: 'GET', headers: headers}, process_response(cb));
};
nodester.prototype.appnpm_handler = function (name, package, action, cb) {
- request({uri: this.baseurl + "appnpm", method: 'POST', headers: headers, body: querystring.stringify({appname: name, package: package, action: action})}, function (err, response, body) {
- cb(err, JSON.parse(body));
- });
+ request({uri: this.baseurl + "appnpm", method: 'POST', headers: headers, body: querystring.stringify({appname: name, package: package, action: action})}, process_response(cb));
};
nodester.prototype.appnpm_install = function (name, package, cb) {

0 comments on commit c6dd3fe

Please sign in to comment.