Commit 90cdc014 authored by Tristan Cavelier's avatar Tristan Cavelier

command resolver and reject behavior improved

parent d6e203b2
......@@ -6,25 +6,14 @@ function restCommandRejecter(param, args) {
// reject(status, reason, {..});
// reject(status, {..});
var a = args[0], b = args[1], c = args[2], d = args[3], weak, strong;
weak = {"result": "error"};
strong = {};
weak.status = constants.http_status.unknown;
weak.statusText = constants.http_status_text.unknown;
weak.message = 'Command failed';
weak.reason = 'fail';
weak.method = param.method;
if (param.kwargs._id) {
weak.id = param.kwargs._id;
}
if (/Attachment$/.test(param.method)) {
weak.attachment = param.kwargs._attachment;
}
weak = {};
strong = {"result": "error"};
// parsing first parameter if is not an object
if (typeof a !== 'object' || Array.isArray(a)) {
strong.status = constants.http_status[a];
strong.statusText = constants.http_status_text[a];
if (strong.status === undefined ||
strong.statusText === undefined) {
if (strong.status === undefined || strong.statusText === undefined) {
return restCommandRejecter(param, [
// can create infernal loop if 'internal_storage_error' is not defined
// in the constants
......@@ -38,23 +27,56 @@ function restCommandRejecter(param, args) {
c = d;
}
// parsing second parameter if is not an object
if (typeof a !== 'object' || Array.isArray(a)) {
strong.reason = a;
a = b;
b = c;
}
// parsing third parameter if is not an object
if (typeof a !== 'object' || Array.isArray(a)) {
strong.message = a;
a = b;
}
// parsing fourth parameter if is an object
if (typeof a === 'object' && !Array.isArray(a)) {
dictUpdate(weak, a);
if (typeof a.reason === 'string' && !strong.reason) {
strong.reason = a.reason;
}
if (typeof a.message === 'string' && !strong.message) {
strong.message = a.message;
}
if ((a.statusText || a.status >= 0) && !strong.statusText) {
strong.status = constants.http_status[a.statusText || a.status];
strong.statusText = constants.http_status_text[a.statusText || a.status];
if (strong.status === undefined || strong.statusText === undefined) {
return restCommandRejecter(param, [
'internal_storage_error',
'invalid response',
'Unknown status "' + (a.statusText || a.status) + '"'
]);
}
}
if (a instanceof Error) {
weak.reason = a.message;
weak.error = a.name;
strong.reason = a.message;
strong.error = a.name;
}
}
// creating defaults
weak.status = constants.http_status.unknown;
weak.statusText = constants.http_status_text.unknown;
weak.message = 'Command failed';
weak.reason = 'fail';
weak.method = param.method;
if (param.kwargs._id) {
weak.id = param.kwargs._id;
}
if (/Attachment$/.test(param.method)) {
weak.attachment = param.kwargs._attachment;
}
dictUpdate(weak, strong);
......
......@@ -6,7 +6,38 @@ function restCommandResolver(param, args) {
// resolve('ok', {"custom": "value"});
// resolve(200, {...});
// resolve({...});
var a = args[0], b = args[1], weak = {"result": "success"}, strong = {};
var a = args[0], b = args[1], weak = {}, strong = {"result": "success"};
// parsing first parameter if is a number or a string
if (typeof a === 'string' || (typeof a === 'number' && isFinite(a))) {
strong.status = constants.http_status[a];
strong.statusText = constants.http_status_text[a];
if (strong.status === undefined || strong.statusText === undefined) {
return restCommandRejecter(param, [
'internal_storage_error',
'invalid response',
'Unknown status "' + a + '"'
]);
}
a = b;
}
// parsing second parameter if is an object
if (typeof a === 'object' && a !== null && !Array.isArray(a)) {
dictUpdate(weak, a);
if ((a.statusText || a.status >= 0) && !strong.statusText) {
strong.status = constants.http_status[a.statusText || a.status];
strong.statusText = constants.http_status_text[a.statusText || a.status];
if (strong.status === undefined || strong.statusText === undefined) {
return restCommandRejecter(param, [
'internal_storage_error',
'invalid response',
'Unknown status "' + (a.statusText || a.status) + '"'
]);
}
}
}
// creating defaults
if (param.method === 'post') {
weak.status = constants.http_status.created;
weak.statusText = constants.http_status_text.created;
......@@ -26,22 +57,6 @@ function restCommandResolver(param, args) {
}
weak.method = param.method;
if (typeof a === 'string' || (typeof a === 'number' && isFinite(a))) {
strong.status = constants.http_status[a];
strong.statusText = constants.http_status_text[a];
if (strong.status === undefined ||
strong.statusText === undefined) {
return restCommandRejecter(param, [
'internal_storage_error',
'invalid response',
'Unknown status "' + a + '"'
]);
}
a = b;
}
if (typeof a === 'object' && !Array.isArray(a)) {
dictUpdate(weak, a);
}
dictUpdate(weak, strong);
strong = undefined; // free memory
if (param.method === 'post' && (typeof weak.id !== 'string' || !weak.id)) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment