Browse files

Pass error objects into callbacks

First step: conforming to node.js standards. Errors, if not surfaced
as separate events to bind to, should be passed as the first parameter
to allow the callback to deal with it meaningfully.

Currently, only the errors from the HTTP layer directly are being passed
(timeouts and such). Further checks can be added to allow callees to
distinguish between success and failure without data.status == 'success'
checks (which, notably, fails in coupon creation).
  • Loading branch information...
1 parent be8a6d4 commit a1df7fb73c19898a42739ebf7cc487e178b3e289 @agnoster agnoster committed Feb 1, 2011
Showing with 13 additions and 11 deletions.
  1. +13 −11 nodester.js
View
24 nodester.js
@@ -19,15 +19,15 @@ var nodester = function (username, password, 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(JSON.parse(body));
+ cb(err, JSON.parse(body));
}
);
};
nodester.prototype.user_create = function (user, pass, email, rsakey, coupon, cb) {
var rsadata = fs.readFileSync(rsakey).toString();
if (rsadata.length < 40) {
- cb("Error: Invalid SSH key file.");
+ cb({ message: "Error: Invalid SSH key file." });
} else {
request({
uri: this.baseurl + "user",
@@ -42,33 +42,33 @@ nodester.prototype.user_create = function (user, pass, email, rsakey, coupon, cb
headers: headers
},
function (err, resp, body) {
- cb(JSON.parse(body))
+ cb(err, JSON.parse(body))
}
);
}
};
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(JSON.parse(body));
+ cb(err, JSON.parse(body));
});
};
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(JSON.parse(body));
+ cb(err, JSON.parse(body));
});
};
nodester.prototype.apps_list = function (cb) {
request({uri: this.baseurl + "apps", method: 'GET'}, function (err, response, body) {
- cb(JSON.parse(body));
+ cb(err, JSON.parse(body));
});
};
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(JSON.parse(body));
+ cb(err, JSON.parse(body));
});
};
@@ -80,7 +80,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(JSON.parse(body));
+ cb(err, JSON.parse(body));
});
};
@@ -97,18 +97,20 @@ 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));
+ });
};
nodester.prototype.app_info = function (name, cb) {
request({uri: this.baseurl + "app/" + name, method: 'GET', headers: headers}, function (err, response, body) {
- cb(JSON.parse(body));
+ cb(err, JSON.parse(body));
});
};
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(JSON.parse(body));
+ cb(err, JSON.parse(body));
});
};

0 comments on commit a1df7fb

Please sign in to comment.