Throw specific errors for missing objects or unknown fields

And add a bit more debugging info to other messages
This commit is contained in:
Dan Stillman 2015-07-20 17:18:34 -04:00
parent 258b70b455
commit b4a8083f2f
2 changed files with 25 additions and 13 deletions

View file

@ -265,7 +265,8 @@ Zotero.Item.prototype.getField = function(field, unformatted, includeBaseMapped)
// Zotero.Items.cacheFields()) or item data has to be loaded
if (this._identified && value === null && !this._loaded.itemData) {
throw new Zotero.Exception.UnloadedDataException(
"Item data not loaded and field '" + field + "' not set", "itemData"
"Item data not loaded and field '" + field + "' not set for item " + this.libraryKey,
"itemData"
);
}
@ -3350,9 +3351,16 @@ Zotero.Item.prototype.setCollections = function (collectionIDsOrKeys) {
// Convert any keys to ids
var collectionIDs = collectionIDsOrKeys.map(function (val) {
return parseInt(val) == val
? parseInt(val)
: this.ContainerObjectsClass.getIDFromLibraryAndKey(this.libraryID, val);
if (parseInt(val) == val) {
return parseInt(val);
}
var id = this.ContainerObjectsClass.getIDFromLibraryAndKey(this.libraryID, val);
if (!id) {
let e = new Error("Collection " + val + " not found for item " + this.libraryKey);
e.name = "ZoteroObjectNotFoundError";
throw e;
}
return id;
}.bind(this));
collectionIDs = Zotero.Utilities.arrayUnique(collectionIDs);
@ -3889,6 +3897,7 @@ Zotero.Item.prototype.fromJSON = Zotero.Promise.coroutine(function* (json) {
case 'accessDate':
case 'dateAdded':
case 'dateModified':
if (val) {
let d = Zotero.Date.isoToDate(val);
if (!d) {
Zotero.logError("Discarding invalid " + field + " '" + val
@ -3896,6 +3905,7 @@ Zotero.Item.prototype.fromJSON = Zotero.Promise.coroutine(function* (json) {
continue;
}
val = Zotero.Date.dateToSQL(d, true);
}
if (field == 'accessDate') {
this.setField(field, val);
}
@ -3945,7 +3955,7 @@ Zotero.Item.prototype.fromJSON = Zotero.Promise.coroutine(function* (json) {
case 'filename':
if (val === "") {
Zotero.logError("Ignoring empty attachment filename in item JSON");
Zotero.logError("Ignoring empty attachment filename in JSON for item " + this.libraryKey);
}
else {
this.attachmentFilename = val;
@ -3964,7 +3974,7 @@ Zotero.Item.prototype.fromJSON = Zotero.Promise.coroutine(function* (json) {
this.itemTypeID
);
if (!isValidForType[field]) {
Zotero.logError("Discarding unknown JSON field " + field);
Zotero.logError("Discarding unknown JSON field " + field + " for item " + this.libraryKey);
continue;
}
this.setField(field, json[field]);

View file

@ -2260,7 +2260,9 @@ Zotero.SearchConditions = new function(){
}
if (!_conditions[condition]){
throw new Error("Invalid condition '" + condition + "' in hasOperator()");
var e = new Error("Invalid condition '" + condition + "' in hasOperator()");
e.name = "ZoteroUnknownFieldError";
throw e;
}
if (!operator && typeof _conditions[condition]['operators'] == 'undefined'){