www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

error.js (4830B)


      1 /*
      2     ***** BEGIN LICENSE BLOCK *****
      3     
      4     Copyright © 2009 Center for History and New Media
      5                      George Mason University, Fairfax, Virginia, USA
      6                      http://zotero.org
      7     
      8     This file is part of Zotero.
      9     
     10     Zotero is free software: you can redistribute it and/or modify
     11     it under the terms of the GNU Affero General Public License as published by
     12     the Free Software Foundation, either version 3 of the License, or
     13     (at your option) any later version.
     14     
     15     Zotero is distributed in the hope that it will be useful,
     16     but WITHOUT ANY WARRANTY; without even the implied warranty of
     17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18     GNU Affero General Public License for more details.
     19     
     20     You should have received a copy of the GNU Affero General Public License
     21     along with Zotero.  If not, see <http://www.gnu.org/licenses/>.
     22     
     23     ***** END LICENSE BLOCK *****
     24 */
     25 
     26 
     27 Zotero.Error = function (message, error, data) {
     28 	this.message = message;
     29 	if (data) {
     30 		for (let prop in data) {
     31 			this[prop] = data[prop];
     32 		}
     33 	}
     34 	if (parseInt(error) == error) {
     35 		this.error = error;
     36 	}
     37 	else {
     38 		this.error = Zotero.Error["ERROR_" + error] ? Zotero.Error["ERROR_" + error] : 0;
     39 	}
     40 }
     41 Zotero.Error.prototype = Object.create(Error.prototype);
     42 Zotero.Error.prototype.name = "Zotero Error";
     43 
     44 Zotero.Error.ERROR_UNKNOWN = 0;
     45 Zotero.Error.ERROR_MISSING_OBJECT = 1;
     46 Zotero.Error.ERROR_FULL_SYNC_REQUIRED = 2;
     47 Zotero.Error.ERROR_API_KEY_NOT_SET = 3;
     48 Zotero.Error.ERROR_API_KEY_INVALID = 4;
     49 Zotero.Error.ERROR_ZFS_OVER_QUOTA = 5;
     50 Zotero.Error.ERROR_ZFS_UPLOAD_QUEUE_LIMIT = 6;
     51 Zotero.Error.ERROR_ZFS_FILE_EDITING_DENIED = 7;
     52 Zotero.Error.ERROR_INVALID_ITEM_TYPE = 8;
     53 Zotero.Error.ERROR_USER_NOT_AVAILABLE = 9;
     54 //Zotero.Error.ERROR_SYNC_EMPTY_RESPONSE_FROM_SERVER = 6;
     55 //Zotero.Error.ERROR_SYNC_INVALID_RESPONSE_FROM_SERVER = 7;
     56 
     57 /**
     58  * Namespace for runtime exceptions
     59  * @namespace
     60  */
     61 Zotero.Exception = {};
     62 
     63 /**
     64  * Encapsulate exceptions with facilities for reporting the underlying cause and
     65  * displaying a dialog with information about the error.
     66  *
     67  * @param {String} name
     68  * @param {String[]} [params]
     69  * @param {String} [title]
     70  * @param {Error|String} [cause]
     71  * @property {String} name The name of the exception. This should correspond to a string 
     72  *     defined in zotero.properties. If it doesn't, it will be displayed as plain text.
     73  * @property {String[]} params Parameters to pass to Zotero.getString() to format the
     74  *     exception, or empty if no parameters.
     75  * @property {String} title The title of the window in which the error will appear. If
     76  *     not specified, the title is "Error."
     77  * @property {Error|String} cause If specified, the report and rethrow methods will
     78  *     operate on this error instead of the displayed error.
     79  */
     80 Zotero.Exception.Alert = function(name, params, title, cause) {
     81 	this.name = name;
     82 	this.params = params || [];
     83 	this._title = title || "general.error";
     84 	this.cause = cause;
     85 };
     86 
     87 Zotero.Exception.Alert.prototype = {
     88 	get title() {
     89 		if(this._title) {
     90 			try {
     91 				return Zotero.getString(this._title);
     92 			} catch(e) {}
     93 		}
     94 		try {
     95 			return Zotero.getString("general.error");
     96 		} catch(e) {
     97 			// Something must be really wrong...
     98 			return "Error";
     99 		}
    100 	},
    101 	
    102 	get message() {
    103 		try {
    104 			return Zotero.getString(this.name, this.params);
    105 		} catch(e) {
    106 			return this.name;
    107 		}
    108 	},
    109 	
    110 	/**
    111 	 * Gets the error string
    112 	 */
    113 	"toString":function() {
    114 		return this.cause ? this.cause.toString() : this.message;
    115 	},
    116 	
    117 	/**
    118 	 * Presents the error in a dialog
    119 	 * @param {DOMWindow} window The window to which the error should be attached
    120 	 */
    121 	"present":function(window) {
    122 		Components.utils.import("resource://gre/modules/Services.jsm");
    123 		try {
    124 			Services.prompt.alert(window || null, this.title, this.message);
    125 		} catch(e) {
    126 			Zotero.debug(e);
    127 		}
    128 	},
    129 	
    130 	/**
    131 	 * Logs the error to the error console
    132 	 */
    133 	"log":function() {
    134 		Zotero.logError(this.cause || this.toString());
    135 	}
    136 };
    137 
    138 /**
    139  * Used to encapsulated cases where the user cancelled an action. This allows us to use
    140  * syntax like "catch (e if e instanceof Zotero.UserCancelledException) {}" to avoid
    141  * doing what we would do with normal exceptions.
    142  */
    143 Zotero.Exception.UserCancelled = function(whatCancelled) {
    144 	this.whatCancelled = whatCancelled || "current operation";
    145 };
    146 Zotero.Exception.UserCancelled.prototype = {
    147 	"name":"UserCancelledException",
    148 	"toString":function() { return "User cancelled "+this.whatCancelled+"."; }
    149 };
    150 
    151 
    152 Zotero.Exception.UnloadedDataException = function (msg, dataType) {
    153 	this.message = msg;
    154 	this.dataType = dataType;
    155 	this.stack = (new Error).stack;
    156 }
    157 Zotero.Exception.UnloadedDataException.prototype = Object.create(Error.prototype);
    158 Zotero.Exception.UnloadedDataException.prototype.name = "UnloadedDataException"