Commit ef828373 authored by Sven Franck's avatar Sven Franck

davStorage: clean up code, remove duplicate error calls

parent f9313d66
...@@ -107,11 +107,54 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -107,11 +107,54 @@ jIO.addStorageType('dav', function (spec, my) {
return clean_url; return clean_url;
}; };
/**
* Replaces "_." with "." in document filenames
* @method restoreDots
* @param {string} url url to clean up
* @return {string} clean_url cleaned up URL
*/
priv.restoreDots = function (url) { priv.restoreDots = function (url) {
var clean_url = url.replace(/_\./g, '.'); var clean_url = url.replace(/_\./g, '.');
return clean_url; return clean_url;
}; };
/**
* Creates error objects for this storage
* @method createError
* @param {string} url url to clean up
* @return {object} error The error object
*/
priv.createError = function (status, message, reason) {
var error = {};
switch (status) {
case 404:
error.status = status;
error.statusText = "Not found";
error.error = "not_found";
error.message = message;
error.reason = reason;
break;
case 405:
error.status = status;
error.statusText = "Method Not Allowed";
error.error = "method_not_allowed";
error.message = message;
error.reason = reason;
break;
case 409:
error.status = status;
error.statusText = "Conflicts";
error.error = "conflicts";
error.message = message;
error.reason = reason;
break;
}
return error;
};
// wedDav methods rfc4918 (short summary) // wedDav methods rfc4918 (short summary)
// COPY Reproduces single resources (files) and collections (directory // COPY Reproduces single resources (files) and collections (directory
// trees). Will overwrite files (if specified by request) but will // trees). Will overwrite files (if specified by request) but will
...@@ -145,25 +188,16 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -145,25 +188,16 @@ jIO.addStorageType('dav', function (spec, my) {
// no docId // no docId
if (!(typeof docid === "string" && docid !== "")) { if (!(typeof docid === "string" && docid !== "")) {
that.error({ that.error(priv.createError(405, "Can't create document without id",
"status": 405, "Document id is undefined"
"statusText": "Method Not Allowed", ));
"error": "method_not_allowed",
"message": "Cannot create document which id is undefined",
"reason": "Document id is undefined"
});
return; return;
} }
// no cross domain ajax // no cross domain ajax
if (priv.checkCors === false) { if (priv.checkCors === false) {
that.error({ that.error(priv.createError(405,
"status": 405, "Browser does not support cross domain ajax", "CORS is undefined"
"statusText": "Method Not Allowed", ));
"error": "method_not_allowed",
"message": "Browser does not support cross domain ajax requests",
"reason": "cors is undefined"
});
return; return;
} }
...@@ -185,13 +219,9 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -185,13 +219,9 @@ jIO.addStorageType('dav', function (spec, my) {
success: function () { success: function () {
if (type === 'POST') { if (type === 'POST') {
// POST the document already exists // POST the document already exists
that.error({ that.error(priv.createError(409,
"status": 409, "Cannot create a new document", "Document already exists"
"statusText": "Conflicts", ));
"error": "conflicts",
"message": "Cannot create a new document",
"reason": "Document already exists"
});
return; return;
} }
// PUT update document // PUT update document
...@@ -214,13 +244,9 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -214,13 +244,9 @@ jIO.addStorageType('dav', function (spec, my) {
}); });
}, },
error: function () { error: function () {
that.error({ that.error(priv.createError(409, "Cannot modify document",
"status": 409, "Error writing to remote storage"
"statusText": "Conflicts", ));
"error": "conflicts",
"message": "Cannot modify document",
"reason": "Error trying to write to remote storage"
});
} }
}); });
}, },
...@@ -246,13 +272,9 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -246,13 +272,9 @@ jIO.addStorageType('dav', function (spec, my) {
}); });
}, },
error: function () { error: function () {
that.error({ that.error(priv.createError(409,
"status": 409, "Cannot modify document", "Error writing to remote storage"
"statusText": "Conflicts", ));
"error": "conflicts",
"message": "Cannot modify document",
"reason": "Error trying to write to remote storage"
});
} }
}); });
...@@ -301,29 +323,19 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -301,29 +323,19 @@ jIO.addStorageType('dav', function (spec, my) {
secured_attachmentid, secured_attachmentid,
attachment_url; attachment_url;
// no docId
if (!(typeof docid === "string" && docid !== "")) { if (!(typeof docid === "string" && docid !== "")) {
that.error({ that.error(priv.createError(405,
"status": 405, "Can't create document without id", "Document id is undefined"
"statusText": "Method Not Allowed", ));
"error": "method_not_allowed",
"message": "Cannot create document which id is undefined",
"reason": "Document id is undefined"
});
return; return;
} }
// no cross domain ajax
if (priv.checkCors === false) { if (priv.checkCors === false) {
that.error({ that.error(priv.createError(405,
"status": 405, "Browser does not support cross domain ajax", "CORS is undefined"
"statusText": "Method Not Allowed", ));
"error": "method_not_allowed",
"message": "Browser does not support cross domain ajax requests",
"reason": "cors is undefined"
});
return; return;
} }
secured_docid = priv.secureDocId(docid); secured_docid = priv.secureDocId(docid);
url = priv.url + '/' + priv.underscoreFileExtenisons(secured_docid); url = priv.url + '/' + priv.underscoreFileExtenisons(secured_docid);
...@@ -385,38 +397,26 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -385,38 +397,26 @@ jIO.addStorageType('dav', function (spec, my) {
}); });
}, },
error: function () { error: function () {
that.error({ that.error(priv.createError(409,
"status": 409, "Cannot modify document", "Error when saving attachment"
"statusText": "Conflicts", ));
"error": "conflicts",
"message": "Cannot modify document",
"reason": "Error trying to save attachment to remote storage"
});
return; return;
} }
}); });
}, },
error: function () { error: function () {
that.error({ that.error(priv.createError(409,
"status": 409, "Cannot modify document", "Error writing to remote storage"
"statusText": "Conflicts", ));
"error": "conflicts",
"message": "Cannot modify document",
"reason": "Error trying to write to remote storage"
});
return; return;
} }
}); });
}, },
error: function () { error: function () {
// the document does not exist // the document does not exist
that.error({ that.error(priv.createError(404,
"status": 404, "Impossible to add attachment", "Document not found"
"statusText": "Not Found", ));
"error": "not_found",
"message": "Impossible to add attachment",
"reason": "Document not found"
});
return; return;
} }
}); });
...@@ -439,26 +439,16 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -439,26 +439,16 @@ jIO.addStorageType('dav', function (spec, my) {
secured_attachmentid, secured_attachmentid,
attachment_url; attachment_url;
// no docId
if (!(typeof docid === "string" && docid !== "")) { if (!(typeof docid === "string" && docid !== "")) {
that.error({ that.error(priv.createError(405,
"status": 405, "Can't create document without id", "Document id is undefined"
"statusText": "Method Not Allowed", ));
"error": "method_not_allowed",
"message": "Cannot create document which id is undefined",
"reason": "Document id is undefined"
});
return; return;
} }
// no cors support
if (priv.checkCors === false) { if (priv.checkCors === false) {
that.error({ that.error(priv.createError(405,
"status": 405, "Browser does not support cross domain ajax", "CORS is undefined"
"statusText": "Method Not Allowed", ));
"error": "method_not_allowed",
"message": "Browser does not support cross domain ajax requests",
"reason": "cors is undefined"
});
return; return;
} }
secured_docid = priv.secureDocId(command.getDocId()); secured_docid = priv.secureDocId(command.getDocId());
...@@ -487,13 +477,9 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -487,13 +477,9 @@ jIO.addStorageType('dav', function (spec, my) {
that.success(doc); that.success(doc);
}, },
error: function () { error: function () {
that.error({ that.error(priv.createError(404,
"status": 404, "Cannot find the attachment", "Attachment does not exist"
"statusText": "Not Found", ));
"error": "not_found",
"message": "Cannot find the attachment",
"reason": "Attachment does not exist"
});
} }
}); });
} else { } else {
...@@ -518,13 +504,9 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -518,13 +504,9 @@ jIO.addStorageType('dav', function (spec, my) {
that.success(doc); that.success(doc);
}, },
error: function () { error: function () {
that.error({ that.error(priv.createError(404,
"status": 404, "Cannot find the document", "Document does not exist"
"statusText": "Not Found", ));
"error": "not_found",
"message": "Cannot find the document",
"reason": "Document does not exist"
});
} }
}); });
} }
...@@ -540,25 +522,17 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -540,25 +522,17 @@ jIO.addStorageType('dav', function (spec, my) {
secured_docid, secured_attachmentid, attachment_url, secured_docid, secured_attachmentid, attachment_url,
attachment_list = [], i, j, k = 1, deleteAttachment; attachment_list = [], i, j, k = 1, deleteAttachment;
// no docId
if (!(typeof docid === "string" && docid !== "")) { if (!(typeof docid === "string" && docid !== "")) {
that.error({ that.error(priv.createError(405,
"status": 405, "Can't create document without id", "Document id is undefined"
"statusText": "Method Not Allowed", ));
"error": "method_not_allowed", return;
"message": "Cannot create document which id is undefined",
"reason": "Document id is undefined"
});
} }
// no cors support
if (priv.checkCors === false) { if (priv.checkCors === false) {
that.error({ that.error(priv.createError(405,
"status": 405, "Browser does not support cross domain ajax", "CORS is undefined"
"statusText": "Method Not Allowed", ));
"error": "method_not_allowed", return;
"message": "Browser does not support cross domain ajax requests",
"reason": "cors is undefined"
});
} }
secured_docid = priv.secureDocId(command.getDocId()); secured_docid = priv.secureDocId(command.getDocId());
...@@ -627,24 +601,16 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -627,24 +601,16 @@ jIO.addStorageType('dav', function (spec, my) {
}); });
}, },
error: function () { error: function () {
that.error({ that.error(priv.createError(409,
"status": 409, "Cannot modify document", "Error saving attachment"
"statusText": "Conflicts", ));
"error": "conflicts",
"message": "Cannot modify document",
"reason": "Error trying to update document attachments"
});
} }
}); });
} else { } else {
// sure this if-else is needed? // sure this if-else is needed?
that.error({ that.error(priv.createError(404,
"status": 404, "Cannot find document", "Error updating attachment"
"statusText": "Not Found", ));
"error": "not_found",
"message": "Cannot find the document",
"reason": "Error trying to update document attachments"
});
} }
} else { } else {
// no attachments, we are done // no attachments, we are done
...@@ -655,24 +621,16 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -655,24 +621,16 @@ jIO.addStorageType('dav', function (spec, my) {
} }
}, },
error: function () { error: function () {
that.error({ that.error(priv.createError(404,
"status": 404, "Cannot find the document", "Document does not exist"
"statusText": "Not Found", ));
"error": "not_found",
"message": "Cannot find the document",
"reason": "Document does not exist"
});
} }
}); });
}, },
error: function () { error: function () {
that.error({ that.error(priv.createError(404,
"status": 404, "Cannot find the attachment", "Error removing attachment"
"statusText": "Not Found", ));
"error": "not_found",
"message": "Cannot find the attachment",
"reason": "Error trying to remove attachment"
});
} }
}); });
// remove document // remove document
...@@ -745,13 +703,9 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -745,13 +703,9 @@ jIO.addStorageType('dav', function (spec, my) {
} }
}, },
error: function () { error: function () {
that.error({ that.error(priv.createError(404,
"status": 404, "Cannot find attachment", "Error removing attachment"
"statusText": "Not Found", ));
"error": "not_found",
"message": "Cannot find the attachment",
"reason": "Error trying to remove attachment"
});
} }
}); });
}; };
...@@ -765,24 +719,16 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -765,24 +719,16 @@ jIO.addStorageType('dav', function (spec, my) {
} }
}, },
error: function () { error: function () {
that.error({ that.error(priv.createError(404,
"status": 404, "Cannot find the document", "Error removing document"
"statusText": "Not Found", ));
"error": "not_found",
"message": "Cannot find the document",
"reason": "Error trying to remove document"
});
} }
}); });
}, },
error: function () { error: function () {
that.error({ that.error(priv.createError(404,
"status": 404, "Cannot find the document", "Document does not exist"
"statusText": "Not Found", ));
"error": "not_found",
"message": "Cannot find the document",
"reason": "Document does not exist"
});
} }
}); });
} }
...@@ -832,13 +778,9 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -832,13 +778,9 @@ jIO.addStorageType('dav', function (spec, my) {
am.call(o, 'success'); am.call(o, 'success');
}, },
error: function (type) { error: function (type) {
that.error({ that.error(priv.createError(404,
"status": 404, "Cannot find the document", "Can't get document from storage"
"statusText": "Not Found", ));
"error": "not_found",
"message": "Cannot find the document",
"reason": "Cannot get a document from DAVStorage"
});
am.call(o, 'error', [type]); am.call(o, 'error', [type]);
} }
}); });
...@@ -887,13 +829,9 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -887,13 +829,9 @@ jIO.addStorageType('dav', function (spec, my) {
}); });
}, },
error: function (type) { error: function (type) {
that.error({ that.error(priv.createError(404,
"status": 404, "Cannot find the document", "Can't get document list"
"statusText": "Not Found", ));
"error": "not_found",
"message": "Cannot find the document",
"reason": "Cannot get a document list from DAVStorage"
});
am.call(o, 'retry', [type]); am.call(o, 'retry', [type]);
} }
}); });
......
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