Skip to content

Commit

Permalink
added create repo api, also improved err handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jutaz committed Oct 15, 2013
1 parent dd9defe commit 0895133
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,37 @@ api.router = function(req, callback) {

api.method_not_allowed = function(req, res) {
res.writeHead(405, {'Content-Type': 'text/plain'});
res.end('Method not allowed');
if(req.errmsg) {
if(typeof req.errmsg !== 'string') {
req.errmsg = req.errmsg.toString();
}
res.end(req.errmsg);
} else {
res.end('Method not allowed');
}
}
api.not_found = function(req, res) {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('404, Not found');
if(req.errmsg) {
if(typeof req.errmsg !== 'string') {
req.errmsg = req.errmsg.toString();
}
res.end(req.errmsg);
} else {
res.end('404, Not found');
}

}
api.bad_request = function(req, res) {
res.writeHead(400, {'Content-Type': 'text/plain'});
if(req.errmsg) {
if(typeof req.errmsg !== 'string') {
req.errmsg = req.errmsg.toString();
}
res.end(req.errmsg);
} else {
res.end('Bad request');
}
}

api.get.repo = function(req, res, git) {
Expand All @@ -68,7 +94,21 @@ api.get.repo = function(req, res, git) {
}

api.post.repo = function(req, res, git) {

try {
req.body.users = JSON.parse(req.body.users);
} catch(e) {
req.errmsg = "Invadid JSON in users array";
api.bad_request(req, res);
return;
}
git.createRepo(req.body, function(err) {
if(err) {
req.errmsg = err;
api.bad_request(req, res);
return;
}
res.end('Repo created');
});
}

api.delete.repo = function(req, res, git) {
Expand Down

0 comments on commit 0895133

Please sign in to comment.