diff --git a/lib/server/auth.js b/lib/server/auth.js index 1ab700e3..228c23bb 100644 --- a/lib/server/auth.js +++ b/lib/server/auth.js @@ -40,11 +40,11 @@ code = code.replace('code=', ''); Util.log(code); - authenticate(code, function(token) { + authenticate(code, function(error, token) { var result = { "token": token }; - Util.log(result); + Util.log(error || result); - Util.exec(callback, result); + Util.exec(callback, error, result); }); }; @@ -71,8 +71,17 @@ GithubAuth.headers = { 'content-length': data.length }; req = https.request(GithubAuth, function(res) { - pipe.getBody(res, function(body) { - Util.exec(callback, qs.parse(body).access_token); + pipe.getBody(res, function(error, body) { + var parsed, token; + + if (!error) { + parsed = qs.parse(body); + + if (parsed) + token = parsed.access_token; + } + + Util.exec(callback, error, token); }); }); diff --git a/lib/server/pipe.js b/lib/server/pipe.js index 9af75266..cdad9a66 100644 --- a/lib/server/pipe.js +++ b/lib/server/pipe.js @@ -12,7 +12,7 @@ '# http://cloudcmd.io' + '\n'); var main = global.cloudcmd.main, - Util = require('util.io'), + Util = main.util, fs = require('fs'), zlib = require('zlib'); diff --git a/lib/server/rest.js b/lib/server/rest.js index 485a227e..94d4ce05 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -93,17 +93,17 @@ * * @param pParams {command, method, body, requrest, response} */ - function sendData(pParams) { + function sendData(params) { var p, isFS, isMD, - ret = main.checkParams(pParams); + ret = main.checkParams(params); if (ret) { - p = pParams; + p = params; isFS = Util.isContainStrAtBegin(p.name, CloudFunc.FS), isMD = Util.isContainStrAtBegin(p.name, '/markdown'); if (isFS) - onFS(pParams); + onFS(params); else if (isMD) markdown.operate(p.name, p.request, function(error, data) { if (error) @@ -117,13 +117,17 @@ switch(p.request.method) { case 'GET': - ret = onGET(pParams); + ret = onGET(params); break; case 'PUT': - pipe.getBody(p.request, function(pBody) { - p.body = pBody; - onPUT(p); + pipe.getBody(p.request, function(error, body) { + if (error) + sendError(params, error); + else { + p.body = body; + onPUT(params); + } }); break; } @@ -169,19 +173,29 @@ break; case 'DELETE': - pipe.getBody(p.request, function(body) { - var files = Util.parseJSON(body); + pipe.getBody(p.request, function(error, body) { + var files; - onDelete(p.name, files, query, function(error, callback) { - checkSendError(error, params, function() { - var names = (files && files.length) ? files : p.name; + if (error) + sendError(p, error); + else { + files = Util.parseJSON(body); + + onDelete(p.name, files, query, function(error, callback) { + var names; - if (callback) - Util.exec(callback); - else - sendMsg(params, 'delete', names); + if (error) + sendError(params,error); + else { + names = (files && files.length) ? files : p.name; + + if (callback) + Util.exec(callback); + else + sendMsg(params, 'delete', names); + } }); - }); + } }); break; } @@ -238,11 +252,14 @@ switch(lCmd) { case 'auth': - main.auth(p.body, function(pTocken) { - send({ - response: p.response, - data: pTocken - }); + main.auth(p.body, function(error, token) { + if (error) + sendError(p, error); + else + send({ + response: p.response, + data: token + }); }); break; diff --git a/lib/server/rest/fs/put.js b/lib/server/rest/fs/put.js index 00128685..56ca4be3 100644 --- a/lib/server/rest/fs/put.js +++ b/lib/server/rest/fs/put.js @@ -52,8 +52,11 @@ if (!error) if (size < MAX_SIZE) - pipe.getBody(readStream, function(patch) { - patchFile(patch); + pipe.getBody(readStream, function(error, patch) { + if (error) + func(error); + else + patchFile(patch); }); else error = 'File is to big. ' + diff --git a/lib/server/rest/markdown.js b/lib/server/rest/markdown.js index f7fb8a69..26f20c23 100644 --- a/lib/server/rest/markdown.js +++ b/lib/server/rest/markdown.js @@ -32,8 +32,11 @@ case 'PUT': - pipe.getBody(request, function(data) { - parse(data, callback); + pipe.getBody(request, function(error, data) { + if (error) + Util.exec(callback, error); + else + parse(data, callback); }); break; }