feature(main) sendError: params, error -> error, params

This commit is contained in:
coderaiser 2014-08-13 06:53:21 -04:00
parent 96c90587bd
commit a7c0c78885
4 changed files with 42 additions and 40 deletions

View file

@ -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);
});

View file

@ -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);
}
});
}
}
);

View file

@ -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);
}

View file

@ -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);
});