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) { ...@@ -6,25 +6,14 @@ function restCommandRejecter(param, args) {
// reject(status, reason, {..}); // reject(status, reason, {..});
// reject(status, {..}); // reject(status, {..});
var a = args[0], b = args[1], c = args[2], d = args[3], weak, strong; var a = args[0], b = args[1], c = args[2], d = args[3], weak, strong;
weak = {"result": "error"}; weak = {};
strong = {}; strong = {"result": "error"};
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;
}
// parsing first parameter if is not an object
if (typeof a !== 'object' || Array.isArray(a)) { if (typeof a !== 'object' || Array.isArray(a)) {
strong.status = constants.http_status[a]; strong.status = constants.http_status[a];
strong.statusText = constants.http_status_text[a]; strong.statusText = constants.http_status_text[a];
if (strong.status === undefined || if (strong.status === undefined || strong.statusText === undefined) {
strong.statusText === undefined) {
return restCommandRejecter(param, [ return restCommandRejecter(param, [
// can create infernal loop if 'internal_storage_error' is not defined // can create infernal loop if 'internal_storage_error' is not defined
// in the constants // in the constants
...@@ -38,25 +27,58 @@ function restCommandRejecter(param, args) { ...@@ -38,25 +27,58 @@ function restCommandRejecter(param, args) {
c = d; c = d;
} }
// parsing second parameter if is not an object
if (typeof a !== 'object' || Array.isArray(a)) { if (typeof a !== 'object' || Array.isArray(a)) {
strong.reason = a; strong.reason = a;
a = b; a = b;
b = c; b = c;
} }
// parsing third parameter if is not an object
if (typeof a !== 'object' || Array.isArray(a)) { if (typeof a !== 'object' || Array.isArray(a)) {
strong.message = a; strong.message = a;
a = b; a = b;
} }
// parsing fourth parameter if is an object
if (typeof a === 'object' && !Array.isArray(a)) { if (typeof a === 'object' && !Array.isArray(a)) {
dictUpdate(weak, 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) { if (a instanceof Error) {
weak.reason = a.message; strong.reason = a.message;
weak.error = a.name; 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); dictUpdate(weak, strong);
strong = undefined; strong = undefined;
if (weak.error === undefined) { if (weak.error === undefined) {
......
...@@ -6,7 +6,38 @@ function restCommandResolver(param, args) { ...@@ -6,7 +6,38 @@ function restCommandResolver(param, args) {
// resolve('ok', {"custom": "value"}); // resolve('ok', {"custom": "value"});
// resolve(200, {...}); // resolve(200, {...});
// resolve({...}); // 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') { if (param.method === 'post') {
weak.status = constants.http_status.created; weak.status = constants.http_status.created;
weak.statusText = constants.http_status_text.created; weak.statusText = constants.http_status_text.created;
...@@ -26,22 +57,6 @@ function restCommandResolver(param, args) { ...@@ -26,22 +57,6 @@ function restCommandResolver(param, args) {
} }
weak.method = param.method; 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); dictUpdate(weak, strong);
strong = undefined; // free memory strong = undefined; // free memory
if (param.method === 'post' && (typeof weak.id !== 'string' || !weak.id)) { 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