From a7c0c78885332f20b8abcca4fe3b7d816774e94d Mon Sep 17 00:00:00 2001 From: coderaiser Date: Wed, 13 Aug 2014 06:53:21 -0400 Subject: [PATCH] feature(main) sendError: params, error -> error, params --- cloudcmd.js | 4 ++-- lib/server/join.js | 20 +++++++++++-------- lib/server/main.js | 10 ++++------ lib/server/rest.js | 48 +++++++++++++++++++++++----------------------- 4 files changed, 42 insertions(+), 40 deletions(-) diff --git a/cloudcmd.js b/cloudcmd.js index d622e044..772ae1f4 100644 --- a/cloudcmd.js +++ b/cloudcmd.js @@ -246,7 +246,7 @@ p.name = path; main.sendFile(p); } else { - main.sendError(p, error); + main.sendError(error, p); } else buildIndex(dir, function(error, data) { @@ -255,7 +255,7 @@ p.name = PATH_INDEX; if (error) - main.sendError(error); + main.sendError(error, p); else main.sendResponse(p, data, NOT_LOG); }); diff --git a/lib/server/join.js b/lib/server/join.js index fa9d2efe..aba9bf0f 100644 --- a/lib/server/join.js +++ b/lib/server/join.js @@ -51,17 +51,21 @@ stream = isGzip ? gzip : res; files.readPipe(names, stream, function(error) { - if (error) - if (!res.headersSent) - main.sendError({ + var msg = ''; + + if (error) { + Util.log(error); + msg = error.message; + + if (res.headersSent) + stream.end(msg); + else + main.sendError(msg, { request : req, response : res, name : path - }, error); - else { - Util.log(error); - stream.end(error.message); - } + }); + } } ); diff --git a/lib/server/main.js b/lib/server/main.js index 8e2942cc..b29d77e9 100644 --- a/lib/server/main.js +++ b/lib/server/main.js @@ -264,7 +264,7 @@ options = {}; if (error) { - sendError(params, error); + sendError(error, params); } else { isGzip = isGZIP(p.request) && p.gzip; time = stat.mtime, @@ -290,7 +290,7 @@ pipe.create(p.name, p.response, options, function(error) { if (error) - sendError(params, error); + sendError(error, params); }); } }); @@ -376,15 +376,13 @@ /** * send error response */ - function sendError(params, error) { + function sendError(error, params) { var p, ret = checkParams(params); if (ret) { p = params; p.status = FILE_NOT_FOUND; - - if (!p.data && error) - p.data = error.message; + p.data = error.message || '' + error; sendResponse(p); } diff --git a/lib/server/rest.js b/lib/server/rest.js index 1ffd10c5..75194257 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -107,7 +107,7 @@ else if (isMD) markdown.operate(p.name, p.request, function(error, data) { if (error) - sendError(p, error); + sendError(error, p); else sendResponse(p, data, NOT_LOG); }); @@ -123,7 +123,7 @@ case 'PUT': pipe.getBody(p.request, function(error, body) { if (error) - sendError(params, error); + sendError(error, params); else { p.body = body; onPUT(params); @@ -162,7 +162,7 @@ if (error.code === 'ENOTDIR') main.sendFile(params); else - sendError(params, error); + sendError(error, params); } else { data.path = format.addSlashToEnd(p.name); params.name += '.json'; @@ -182,7 +182,7 @@ case 'PUT': onFSPut(query, path, p.request, function(error, msg) { if (error) - sendError(params, error); + sendError(error, params); else sendResponse(params, msg); }); @@ -193,7 +193,7 @@ var files; if (error) - sendError(p, error); + sendError(error, p); else { files = Util.parseJSON(body); @@ -201,7 +201,7 @@ var names; if (error) { - sendError(params,error); + sendError(error, params); } else { if (files && files.length) names = files; @@ -249,11 +249,11 @@ break; default: - json = Util.stringifyJSON({ - error: 'command not found' - }); + json = { + message: 'Error: command not found!' + }; - sendError(p, json); + sendError(json, p); break; } } @@ -279,7 +279,7 @@ case 'auth': main.auth(p.body, function(error, token) { if (error) - sendError(p, error); + sendError(error, p); else send({ response: p.response, @@ -290,11 +290,11 @@ case 'mv': if (!files.from || !files.to) { - sendError(params, p.data); + sendError(p.data, params); } else if (isRootWin32(files.to)) { - sendError(params, getWin32RootMsg('to')); + sendError(getWin32RootMsg('to'), params); } else if (isRootWin32(files.from)) { - sendError(params, getWin32RootMsg('from')); + sendError(getWin32RootMsg('from'), params); } else { files.from = mellow.convertPath(files.from); files.to = mellow.convertPath(files.to); @@ -306,7 +306,7 @@ copyFiles(files, flop.move, function(error) { if (error) - sendError(params, error); + sendError(error, params); else sendMsg(params, 'move', data); }); @@ -316,11 +316,11 @@ case 'cp': if (!files.from || !files.names || !files.to) { - sendError(params, p.data); + sendError(p.data, params); } else if (isRootWin32(files.to)) { - sendError(params, getWin32RootMsg('to')); + sendError(getWin32RootMsg('to'), params); } else if (isRootWin32(files.from)) { - sendError(params, getWin32RootMsg('from')); + sendError(getWin32RootMsg('from'), params); } else { files.from = mellow.convertPath(files.from); files.to = mellow.convertPath(files.to); @@ -328,7 +328,7 @@ files.namesAll = Util.slice(files.names); copyFiles(files, flop.copy, function(error) { if (error) - sendError(params, error); + sendError(error, params); else sendMsg(params, 'copy', files.namesAll); }); @@ -337,7 +337,7 @@ case 'zip': if (!files.from) { - sendError(params, p.data); + sendError(p.data, params); } else { from = mellow.convertPath(files.from); @@ -350,7 +350,7 @@ var name = path.basename(files.from); if (error) - sendError(params, error); + sendError(error, params); else sendMsg(params, 'zip', name); }); @@ -359,7 +359,7 @@ case 'unzip': if (!files.from) { - sendError(params, p.data); + sendError(p.data, params); } else { from = mellow.convertPath(files.from); @@ -372,7 +372,7 @@ var name = path.basename(files.from); if (error) - sendError(params, error); + sendError(error, params); else sendMsg(params, 'unzip', name); }); @@ -398,7 +398,7 @@ fs.writeFile(JSONDIR + 'config.json', json, function(error) { if (error) - sendError(params, error); + sendError(error, params); else sendMsg(params, 'config', name); });