From 6021214b062c329a35cff3ac898f00668723fb4a Mon Sep 17 00:00:00 2001 From: Simon Kornblith Date: Fri, 17 Feb 2012 23:10:18 -0500 Subject: [PATCH] Further restructuring of error handling --- chrome/content/zotero/xpcom/integration.js | 206 +++++++++++++-------- 1 file changed, 128 insertions(+), 78 deletions(-) diff --git a/chrome/content/zotero/xpcom/integration.js b/chrome/content/zotero/xpcom/integration.js index b7fd147d6..87840ce8c 100644 --- a/chrome/content/zotero/xpcom/integration.js +++ b/chrome/content/zotero/xpcom/integration.js @@ -1246,7 +1246,10 @@ Zotero.Integration.Fields.prototype._retrieveFields = function() { * Shows an error if a field code is corrupted * @param {Exception} e The exception thrown * @param {Field} field The Zotero field object + * @param {Function} callback The callback passed to updateSession + * @param {Function} errorCallback The error callback passed to updateSession * @param {Integer} i The field index + * @return {Boolean} Whether to continue updating the session */ Zotero.Integration.Fields.prototype._showCorruptFieldError = function(e, field, callback, errorCallback, i) { Zotero.logError(e); @@ -1277,6 +1280,58 @@ Zotero.Integration.Fields.prototype._showCorruptFieldError = function(e, field, } } +/** + * Shows an error if a field code is missing + * @param {Exception} e The exception thrown + * @param {Exception} e The exception thrown + * @param {Field} field The Zotero field object + * @param {Function} callback The callback passed to updateSession + * @param {Function} errorCallback The error callback passed to updateSession + * @param {Integer} i The field index + * @return {Boolean} Whether to continue updating the session + */ +Zotero.Integration.Fields.prototype._showMissingItemError = function(e, field, callback, errorCallback, i) { + // First, check if we've already decided to remove field codes from these + var reselect = true; + for each(var reselectKey in e.reselectKeys) { + if(this._deleteKeys[reselectKey]) { + this._removeCodeFields.push(i); + return true; + } + } + + // Ask user what to do with this item + if(e.citationLength == 1) { + var msg = Zotero.getString("integration.missingItem.single"); + } else { + var msg = Zotero.getString("integration.missingItem.multiple", (e.citationIndex+1).toString()); + } + msg += '\n\n'+Zotero.getString('integration.missingItem.description'); + field.select(); + this._doc.activate(); + var result = this._doc.displayAlert(msg, 1, 3); + if(result == 0) { // Cancel + throw new Zotero.Integration.UserCancelledException(); + } else if(result == 1) { // No + for each(var reselectKey in e.reselectKeys) { + this._deleteKeys[reselectKey] = true; + } + this._removeCodeFields.push(i); + return true; + } else { // Yes + // Display reselect item dialog + var me = this; + var oldCurrentWindow = Zotero.Integration.currentWindow; + this._session.reselectItem(this._doc, e, function() { + // Now try again + Zotero.Integration.currentWindow = oldCurrentWindow; + me._doc.activate(); + me._processFields(me._fields, callback, errorCallback, i); + }); + return false; + } +} + /** * Updates Zotero.Integration.Session attached to Zotero.Integration.Fields in line with document */ @@ -1305,22 +1360,28 @@ Zotero.Integration.Fields.prototype.updateSession = function(callback, errorCall try { me._session.loadBibliographyData(me._bibliographyData); } catch(e) { - if(errorCallback && !errorCallback(e)) { - } else if(e instanceof Zotero.Integration.CorruptFieldException) { - var msg = Zotero.getString("integration.corruptBibliography")+'\n\n'+ - Zotero.getString('integration.corruptBibliography.description'); - var result = me._doc.displayAlert(msg, - Components.interfaces.zoteroIntegrationDocument.DIALOG_ICON_CAUTION, - Components.interfaces.zoteroIntegrationDocument.DIALOG_BUTTONS_OK_CANCEL); - if(result == 0) { - throw e; + var defaultHandler = function() { + if(e instanceof Zotero.Integration.CorruptFieldException) { + var msg = Zotero.getString("integration.corruptBibliography")+'\n\n'+ + Zotero.getString('integration.corruptBibliography.description'); + var result = me._doc.displayAlert(msg, + Components.interfaces.zoteroIntegrationDocument.DIALOG_ICON_CAUTION, + Components.interfaces.zoteroIntegrationDocument.DIALOG_BUTTONS_OK_CANCEL); + if(result == 0) { + throw e; + } else { + me._bibliographyData = ""; + me._session.bibliographyHasChanged = true; + me._session.bibliographyDataHasChanged = true; + } } else { - me._bibliographyData = ""; - me._session.bibliographyHasChanged = true; - me._session.bibliographyDataHasChanged = true; + throw e; } - } else { - throw e; + }; + if(errorCallback) { + if(!errorCallback(e, defaultHandler)) return; + } else if(!defaultHandler()) { + return; } } } @@ -1351,13 +1412,26 @@ Zotero.Integration.Fields.prototype.updateSession = function(callback, errorCall Zotero.Integration.Fields.prototype._processFields = function(fields, callback, errorCallback, i) { if(!i) i = 0; + var me = this; for(var n = fields.length; i