Commit 447c8c4d authored by Tristan Cavelier's avatar Tristan Cavelier

Enhance error detection accuracy

parent 73508dd2
...@@ -21,7 +21,7 @@ var command = function(spec, my) { ...@@ -21,7 +21,7 @@ var command = function(spec, my) {
priv.tried = 0; priv.tried = 0;
priv.doc = spec.doc || {}; priv.doc = spec.doc || {};
priv.docid = spec.docid || spec.doc._id || ''; priv.docid = spec.docid || priv.doc._id;
priv.option = spec.options || {}; priv.option = spec.options || {};
priv.callbacks = spec.callbacks || {}; priv.callbacks = spec.callbacks || {};
priv.success = priv.callbacks.success || function (){}; priv.success = priv.callbacks.success || function (){};
...@@ -75,6 +75,9 @@ var command = function(spec, my) { ...@@ -75,6 +75,9 @@ var command = function(spec, my) {
* @return {string} The document id * @return {string} The document id
*/ */
that.getDocId = function () { that.getDocId = function () {
if (typeof priv.docid !== "string") {
return undefined;
}
return priv.docid.split('/')[0]; return priv.docid.split('/')[0];
}; };
...@@ -84,6 +87,9 @@ var command = function(spec, my) { ...@@ -84,6 +87,9 @@ var command = function(spec, my) {
* @return {string} The attachment id * @return {string} The attachment id
*/ */
that.getAttachmentId = function () { that.getAttachmentId = function () {
if (typeof priv.docid !== "string") {
return undefined;
}
return priv.docid.split('/')[1]; return priv.docid.split('/')[1];
}; };
...@@ -157,7 +163,8 @@ var command = function(spec, my) { ...@@ -157,7 +163,8 @@ var command = function(spec, my) {
* @param {object} storage The storage. * @param {object} storage The storage.
*/ */
that.validate = function (storage) { that.validate = function (storage) {
if (!priv.docid.match(/^[^\/]+([\/][^\/]+)?$/)) { if (typeof priv.docid === "string" &&
!priv.docid.match(/^[^\/]+([\/][^\/]+)?$/)) {
that.error({ that.error({
status:21,statusText:'Invalid Document Id', status:21,statusText:'Invalid Document Id',
error:'invalid_document_id', error:'invalid_document_id',
......
...@@ -9,7 +9,7 @@ var getCommand = function(spec, my) { ...@@ -9,7 +9,7 @@ var getCommand = function(spec, my) {
}; };
that.validateState = function() { that.validateState = function() {
if (typeof that.getDocId() === "string" && that.getDocId() === "") { if (!(typeof that.getDocId() === "string" && that.getDocId() !== "")) {
that.error({ that.error({
"status": 20, "status": 20,
"statusText": "Document Id Required", "statusText": "Document Id Required",
......
...@@ -15,7 +15,7 @@ var postCommand = function(spec, my) { ...@@ -15,7 +15,7 @@ var postCommand = function(spec, my) {
that.error({ that.error({
"status": 21, "status": 21,
"statusText": "Invalid Document Id", "statusText": "Invalid Document Id",
"error": "Invalid Document Id", "error": "invalid_document_id",
"message": "The document id contains '/' characters "+ "message": "The document id contains '/' characters "+
"which are forbidden", "which are forbidden",
"reason": "Document id contains '/' character(s)" "reason": "Document id contains '/' character(s)"
......
...@@ -13,7 +13,7 @@ var putAttachmentCommand = function(spec, my) { ...@@ -13,7 +13,7 @@ var putAttachmentCommand = function(spec, my) {
}; };
that.validateState = function () { that.validateState = function () {
if (typeof that.getAttachmentId() === "undefined") { if (typeof that.getAttachmentId() !== "string") {
that.error({ that.error({
"status": 22, "status": 22,
"statusText": "Attachment Id Required", "statusText": "Attachment Id Required",
......
...@@ -11,7 +11,7 @@ var putCommand = function(spec, my) { ...@@ -11,7 +11,7 @@ var putCommand = function(spec, my) {
}; };
that.validateState = function () { that.validateState = function () {
if (typeof that.getDocId() === "string" && that.getDocId() === "") { if (!(typeof that.getDocId() === "string" && that.getDocId() !== "")) {
that.error({ that.error({
"status": 20, "status": 20,
"statusText": "Document Id Required", "statusText": "Document Id Required",
......
...@@ -8,6 +8,32 @@ var removeCommand = function(spec, my) { ...@@ -8,6 +8,32 @@ var removeCommand = function(spec, my) {
return 'remove'; return 'remove';
}; };
that.validateState = function() {
if (!(typeof that.getDocId() === "string" && that.getDocId() !== "")) {
that.error({
"status": 20,
"statusText": "Document Id Required",
"error": "document_id_required",
"message": "The document id is not provided",
"reason": "Document id is undefined"
});
return false;
}
if (typeof that.getAttachmentId() === "string") {
if (that.getAttachmentId() === "") {
that.error({
"status": 23,
"statusText": "Invalid Attachment Id",
"error": "invalid_attachment_id",
"message": "The attachment id must not be an empty string",
"reason": "Attachment id is empty"
});
}
return false;
}
return true;
};
that.executeOn = function(storage) { that.executeOn = function(storage) {
storage.remove (that); storage.remove (that);
}; };
......
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