Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Wrapped all JSON.parse calls #43

Merged
1 commit merged into from

1 participant

@davglass

Wrapped all JSON.parse calls with a try/catch since JSON.parse throws on an invalid JSON string.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 11, 2011
  1. @davglass
This page is out of date. Refresh to see the latest.
View
33 lib/app.js
@@ -176,14 +176,18 @@ module.exports = {
var user = req.user;
request({ uri: config.couch_loc + 'apps/' + appname, method:'GET', headers: config.couch_headers }, function (err, response, body) {
- var myObject = JSON.parse(body);
+ try {
+ var myObject = JSON.parse(body);
+ } catch (e) {}
if (myObject._id){
// subdomain already exists
res.send({ "status": "failure - appname exists" });
} else {
// subdomain available - get next available port address
request({ uri: config.couch_loc + 'nextport/port', method:'GET', headers: config.couch_headers }, function (err, response, body) {
- var doc = JSON.parse(body);
+ try {
+ var doc = JSON.parse(body);
+ } catch (e) {}
var appport = 8000;
if (!doc.error || doc.error == 'not_found') {
appport = doc.address
@@ -191,10 +195,16 @@ module.exports = {
var repo_id = doc._rev;
// increment next port address
request({ uri: config.couch_loc + 'nextport/port', method:'PUT', body: JSON.stringify({_id: "port", address: appport + 1, _rev: doc._rev }), headers: config.couch_headers }, function (err, response, body) {
- var doc = JSON.parse(body);
+ try {
+ var doc = JSON.parse(body);
+ } catch (e) {}
+
// Create the app
request({ uri: config.couch_loc + 'apps', method:'POST', body: JSON.stringify({_id: appname, start: start, port: appport, username: user._id, repo_id: repo_id, running: false, pid: 'unknown' }), headers: config.couch_headers }, function (err, response, body) {
- var doc = JSON.parse(body);
+ try {
+ var doc = JSON.parse(body);
+ } catch (e) {}
+
request({ uri: config.couch_loc + 'repos', method:'POST', body: JSON.stringify({_id: repo_id, appname: appname, username: user._id}), headers: config.couch_headers }, function (err, response, body) {
// TODO - Error handling...
});
@@ -239,7 +249,10 @@ var force_stop = function(repo_id, callback) {
var app_stop = function (repo_id, callback) {
request({ uri: config.couch_loc + 'repos/' + repo_id, method:'GET', headers: config.couch_headers }, function (err, response, body) {
- var doc = JSON.parse(body);
+ try {
+ var doc = JSON.parse(body);
+ } catch (e) {}
+
if (typeof doc.error != 'undefined' && doc.error == 'not_found') {
callback(false);
} else {
@@ -269,14 +282,20 @@ var app_stop = function (repo_id, callback) {
var app_start = function (repo_id, callback) {
request({ uri: config.couch_loc + 'repos/' + repo_id, method:'GET', headers: config.couch_headers }, function (err, response, body) {
- var doc = JSON.parse(body);
+ try {
+ var doc = JSON.parse(body);
+ } catch (e) {}
+
if (typeof doc.error != 'undefined' && doc.error == 'not_found') {
callback(false);
} else {
var user_home = path.join(config.opt.home_dir, config.opt.hosted_apps_subdir, doc.username);
var app_home = user_home + '/' + repo_id;
request({ method: 'GET', uri: config.couch_loc + 'apps/' + doc.appname, headers: config.couch_headers }, function (err, response, body) {
- var app = JSON.parse(body);
+ try {
+ var app = JSON.parse(body);
+ } catch (e) {}
+
if (typeof app.error != 'undefined' && app.error == 'not_found') {
callback(false);
} else {
View
45 lib/apps.js
@@ -6,28 +6,31 @@ module.exports = {
var user = req.user;
request({ method: 'GET', uri: config.couch_loc + 'apps/' + '/_design/nodeapps/_view/all', headers: config.couch_headers }, function (err, response, body) {
- var docs = JSON.parse(body);
- if (docs) { // Maybe better error handling here
- var apps = [];
- var i;
- for (i=0; i<docs.rows.length; i++) {
- if (user._id == docs.rows[i].value.username) {
- apps.push({
- name: docs.rows[i].id
- , port: docs.rows[i].value.port
- , gitrepo: config.opt.git_user + '@' + config.opt.git_dom + ':' + config.opt.home_dir + '/' + config.opt.hosted_apps_subdir + '/' + docs.rows[i].value.username + '/' + docs.rows[i].value.repo_id + '.git'
- , start: docs.rows[i].value.start
- , running: docs.rows[i].value.running
- , pid: docs.rows[i].value.pid
- });
- }
+ try {
+ var docs = JSON.parse(body);
+ } catch (e) {}
+
+ if (docs) { // Maybe better error handling here
+ var apps = [];
+ var i;
+ for (i=0; i<docs.rows.length; i++) {
+ if (user._id == docs.rows[i].value.username) {
+ apps.push({
+ name: docs.rows[i].id
+ , port: docs.rows[i].value.port
+ , gitrepo: config.opt.git_user + '@' + config.opt.git_dom + ':' + config.opt.home_dir + '/' + config.opt.hosted_apps_subdir + '/' + docs.rows[i].value.username + '/' + docs.rows[i].value.repo_id + '.git'
+ , start: docs.rows[i].value.start
+ , running: docs.rows[i].value.running
+ , pid: docs.rows[i].value.pid
+ });
+ }
+ }
+ res.send(apps);
+ } else {
+ res.writeHead(404, { 'Content-Type': 'application/json' });
+ res.write('{"status" : "failure - applications not found"}');
+ res.end();
}
- res.send(apps);
- } else {
- res.writeHead(404, { 'Content-Type': 'application/json' });
- res.write('{"status" : "failure - applications not found"}');
- res.end();
- }
});
}
View
4 lib/coupon.js
@@ -16,7 +16,9 @@ module.exports = {
//I don't have this view, so I can't test it..
unsent: function(req, res, next) {
request({ uri: config.couch_loc + 'coupons/_design/coupons/_view/unsent', method:'GET', headers: config.couch_headers }, function (err, response, body) {
- var doc = JSON.parse(body);
+ try {
+ var doc = JSON.parse(body);
+ } catch (e) {}
var buff = "";
for(var i in doc.rows) {
// sys.puts(doc.rows[i].id);
View
10 lib/domains.js
@@ -15,7 +15,10 @@ module.exports = {
var gooddomain = lib.checkDomain(domain);
if (gooddomain === true) {
request({ uri: config.couch_loc + 'aliasdomains/' + domain, method:'GET', headers: config.couch_headers }, function (err, response, body) {
- var doc = JSON.parse(body);
+ try {
+ var doc = JSON.parse(body);
+ } catch (e) {}
+
if (doc._id){
res.error(400, '{"status": "failure - domain already exists"}\n');
} else {
@@ -32,7 +35,10 @@ module.exports = {
var gooddomain = lib.checkDomain(domain);
if (gooddomain === true) {
request({uri: config.couch_loc + 'aliasdomains/' + domain, method:'GET', headers: config.couch_headers }, function (err, response, body) {
- var doc = JSON.parse(body);
+ try {
+ var doc = JSON.parse(body);
+ } catch (e) {}
+
if (doc._id) {
if (doc.appname == appname) {
request({uri:couch_loc + 'aliasdomains/' + domain + '?rev=' + doc._rev, method:'DELETE', headers:h}, function (err, response, body) {
View
5 lib/lib.js
@@ -13,7 +13,10 @@ var couchdb_parse = function (err, body, cb) {
if (err != null) {
cb(err, null);
} else {
- var doc = JSON.parse(body);
+ try {
+ var doc = JSON.parse(body);
+ } catch (e) {}
+
if (doc.hasOwnProperty('error')) {
cb(doc.error, doc);
} else {
View
8 lib/middle.js
@@ -16,7 +16,9 @@ var authenticate = function(req, res, next) {
var password = creds.substring(creds.indexOf(":")+1);
request({uri: config.couch_loc + 'nodefu/' + username, method:'GET', headers: config.couch_headers }, function (err, response, body) {
- var doc = JSON.parse(body);
+ try {
+ var doc = JSON.parse(body);
+ } catch (e) {}
if (doc && doc._id == username && doc.password == config.md5(password)) {
req.user = doc;
next();
@@ -50,7 +52,9 @@ var authenticate_app = function(req, res, next) {
if (req.user) {
request({ method: 'GET', uri: config.couch_loc + 'apps/' + appname, headers: config.couch_headers }, function (err, response, body) {
- var doc = JSON.parse(body);
+ try {
+ var doc = JSON.parse(body);
+ } catch (e) {}
if (doc && doc.username == req.user._id) {
req.appname = appname;
req.repo = req.app = doc;
View
5 lib/user.js
@@ -47,7 +47,10 @@ module.exports = {
if (req.body.coupon == config.opt.coupon_code) {
request({ uri: config.couch_loc + 'nodefu/' + req.body.user, method: 'GET', headers: config.couch_headers}, function (err, response, body) {
- var doc = JSON.parse(body);
+ try {
+ var doc = JSON.parse(body);
+ } catch (e) {}
+
if (doc._id){
// account already registered
res.writeHead(400, { 'Content-Type': 'application/json' });
Something went wrong with that request. Please try again.