Commit 069263fb authored by Tristan Cavelier's avatar Tristan Cavelier

restCommandResolver and Rejected behavior improved

parent d1f84785
...@@ -5,91 +5,101 @@ function restCommandRejecter(param, args) { ...@@ -5,91 +5,101 @@ function restCommandRejecter(param, args) {
// reject(status, reason, message, {"custom": "value"}); // reject(status, reason, message, {"custom": "value"});
// 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 arg, current_priority, priority = [
weak = {}; // 0 - custom parameter values
strong = {"result": "error"}; {},
// 1 - default values
{
"status": constants.http_status.unknown,
"statusText": constants.http_status_text.unknown,
"message": "Command failed",
"reason": "unknown"
},
// 2 - status, reason, message parameters
{},
// 3 - never change
{"result": "error", "method": param.method}
];
args = Array.prototype.slice.call(args);
arg = args.shift();
// parsing first parameter if is not an object // priority 3 - never change
if (typeof a !== 'object' || Array.isArray(a)) { current_priority = priority[3];
strong.status = constants.http_status[a]; if (param.kwargs._id) {
strong.statusText = constants.http_status_text[a]; current_priority.id = param.kwargs._id;
if (strong.status === undefined || strong.statusText === undefined) {
return restCommandRejecter(param, [
// can create infernal loop if 'internal_storage_error' is not defined
// in the constants
'internal_storage_error',
'invalid response',
'Unknown status "' + a + '"'
]);
} }
a = b; if (/Attachment$/.test(param.method)) {
b = c; current_priority.attachment = param.kwargs._attachment;
c = d;
} }
// priority 2 - status, reason, message parameters
current_priority = priority[2];
// parsing first parameter if is not an object
if (typeof arg !== 'object' || arg === null || Array.isArray(arg)) {
// first parameter is mandatory
current_priority.status = arg;
arg = args.shift();
}
// parsing second parameter if is not an object // parsing second parameter if is not an object
if (typeof a !== 'object' || Array.isArray(a)) { if (typeof arg !== 'object' || arg === null || Array.isArray(arg)) {
strong.reason = a; if (arg !== undefined) {
a = b; current_priority.reason = arg;
b = c; }
arg = args.shift();
} }
// parsing third parameter if is not an object // parsing third parameter if is not an object
if (typeof a !== 'object' || Array.isArray(a)) { if (typeof arg !== 'object' || arg === null || Array.isArray(arg)) {
strong.message = a; if (arg !== undefined) {
a = b; current_priority.message = arg;
}
arg = args.shift();
} }
// priority 0 - custom values
current_priority = priority[0];
// parsing fourth parameter if is an object // parsing fourth parameter if is an object
if (typeof a === 'object' && !Array.isArray(a)) { if (typeof arg === 'object' && arg !== null && !Array.isArray(arg)) {
dictUpdate(weak, a); dictUpdate(current_priority, arg);
if (typeof a.reason === 'string' && !strong.reason) { if (arg.hasOwnProperty('reason')) {
strong.reason = a.reason; current_priority.reason = arg.reason;
}
if (typeof a.message === 'string' && !strong.message) {
strong.message = a.message;
} }
if ((a.statusText || a.status >= 0) && !strong.statusText) { if (arg.hasOwnProperty('message')) {
strong.status = constants.http_status[a.statusText || a.status]; current_priority.message = arg.message;
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 ((arg.statusText || arg.status >= 0)) {
current_priority.status = arg.statusText || arg.status;
} }
if (a instanceof Error) { if (arg instanceof Error) {
strong.reason = a.message; current_priority.reason = arg.message || "";
strong.error = a.name; current_priority.error = arg.name;
} }
} }
// creating defaults // merge priority dicts
weak.status = constants.http_status.unknown; for (current_priority = priority.length - 1;
weak.statusText = constants.http_status_text.unknown; current_priority > 0;
weak.message = 'Command failed'; current_priority -= 1) {
weak.reason = 'fail'; dictUpdate(priority[current_priority - 1], priority[current_priority]);
weak.method = param.method;
if (param.kwargs._id) {
weak.id = param.kwargs._id;
} }
if (/Attachment$/.test(param.method)) { priority = priority[0];
weak.attachment = param.kwargs._attachment;
// check status
priority.statusText = constants.http_status_text[priority.status];
if (priority.statusText === undefined) {
return restCommandRejecter(param, [
// can create infernal loop if 'internal_storage_error' is not defined in
// the constants
'internal_storage_error',
'invalid response',
'Unknown status "' + priority.status + '"'
]);
} }
priority.status = constants.http_status[priority.statusText];
dictUpdate(weak, strong); // set default priority error if not already set
strong = undefined; if (priority.error === undefined) {
if (weak.error === undefined) { priority.error = priority.statusText.toLowerCase().replace(/ /g, '_').
weak.error = weak.statusText.toLowerCase().replace(/ /g, '_').
replace(/[^_a-z]/g, ''); replace(/[^_a-z]/g, '');
} }
if (typeof weak.message !== 'string') { return param.solver.reject(deepClone(priority));
weak.message = "";
}
if (typeof weak.reason !== 'string') {
weak.reason = "unknown";
}
return param.solver.reject(deepClone(weak));
} }
...@@ -6,90 +6,116 @@ function restCommandResolver(param, args) { ...@@ -6,90 +6,116 @@ 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 = {}, strong = {"result": "success"}; var arg, current_priority, priority = [
// 0 - custom parameter values
{},
// 1 - default values
{},
// 2 - status parameter
{},
// 3 - never change
{"result": "success", "method": param.method}
];
args = Array.prototype.slice.call(args);
arg = args.shift();
// parsing first parameter if is a number or a string // priority 3 - never change
if (typeof a === 'string' || (typeof a === 'number' && isFinite(a))) { current_priority = priority[3];
strong.status = constants.http_status[a]; if (param.kwargs._id) {
strong.statusText = constants.http_status_text[a]; current_priority.id = param.kwargs._id;
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) + '"'
]);
}
} }
if (/Attachment$/.test(param.method)) {
current_priority.attachment = param.kwargs._attachment;
} }
// creating defaults // priority 1 - default values
current_priority = priority[1];
if (param.method === 'post') { if (param.method === 'post') {
weak.status = constants.http_status.created; current_priority.status = constants.http_status.created;
weak.statusText = constants.http_status_text.created; current_priority.statusText = constants.http_status_text.created;
} else if (methodType(param.method) === "writer" || } else if (methodType(param.method) === "writer" ||
param.method === "check") { param.method === "check") {
weak.status = constants.http_status.no_content; current_priority.status = constants.http_status.no_content;
weak.statusText = constants.http_status_text.no_content; current_priority.statusText = constants.http_status_text.no_content;
} else { } else {
weak.status = constants.http_status.ok; current_priority.status = constants.http_status.ok;
weak.statusText = constants.http_status_text.ok; current_priority.statusText = constants.http_status_text.ok;
} }
if (param.kwargs._id) {
weak.id = param.kwargs._id; // priority 2 - status parameter
current_priority = priority[2];
// parsing first parameter if is not an object
if (typeof arg !== 'object' || arg === null || Array.isArray(arg)) {
if (arg !== undefined) {
current_priority.status = arg;
} }
if (/Attachment$/.test(param.method)) { arg = args.shift();
weak.attachment = param.kwargs._attachment; }
// priority 0 - custom values
current_priority = priority[0];
// parsing second parameter if is an object
if (typeof arg === 'object' && arg !== null && !Array.isArray(arg)) {
dictUpdate(current_priority, arg);
}
// merge priority dicts
for (current_priority = priority.length - 1;
current_priority > 0;
current_priority -= 1) {
dictUpdate(priority[current_priority - 1], priority[current_priority]);
} }
weak.method = param.method; priority = priority[0];
dictUpdate(weak, strong); // check document id if post method
strong = undefined; // free memory if (param.method === 'post' &&
if (param.method === 'post' && (typeof weak.id !== 'string' || !weak.id)) { (typeof priority.id !== 'string' || !priority.id)) {
return restCommandRejecter(param, [ return restCommandRejecter(param, [
'internal_storage_error', 'internal_storage_error',
'invalid response', 'invalid response',
'New document id have to be specified' 'New document id have to be specified'
]); ]);
} }
// check status
priority.statusText = constants.http_status_text[priority.status];
if (priority.statusText === undefined) {
return restCommandRejecter(param, [
'internal_storage_error',
'invalid response',
'Unknown status "' + priority.status + '"'
]);
}
priority.status = constants.http_status[priority.statusText];
// check data for get Attachment
if (param.method === 'getAttachment') { if (param.method === 'getAttachment') {
if (typeof weak.data === 'string') { if (typeof priority.data === 'string') {
weak.data = new Blob([weak.data], { priority.data = new Blob([priority.data], {
"type": weak.content_type || weak.mimetype || "" "type": priority.content_type || priority.mimetype || ""
}); });
delete weak.content_type; delete priority.content_type;
delete weak.mimetype; delete priority.mimetype;
} }
if (!(weak.data instanceof Blob)) { if (!(priority.data instanceof Blob)) {
return restCommandRejecter(param, [ return restCommandRejecter(param, [
'internal_storage_error', 'internal_storage_error',
'invalid response', 'invalid response',
'getAttachment method needs a Blob as returned "data".' 'getAttachment method needs a Blob as returned "data".'
]); ]);
} }
// check data for readers (except check method)
} else if (methodType(param.method) === 'reader' && } else if (methodType(param.method) === 'reader' &&
param.method !== 'check' && param.method !== 'check' &&
(typeof weak.data !== 'object' || (typeof priority.data !== 'object' ||
Object.getPrototypeOf(weak.data) !== Object.prototype)) { priority.data === null ||
Object.getPrototypeOf(priority.data) !== Object.prototype)) {
return restCommandRejecter(param, [ return restCommandRejecter(param, [
'internal_storage_error', 'internal_storage_error',
'invalid response', 'invalid response',
param.method + ' method needs a dict as returned "data".' param.method + ' method needs a dict as returned "data".'
]); ]);
} }
return param.solver.resolve(deepClone(weak));
return param.solver.resolve(deepClone(priority));
} }
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