Source: core/kekule.exceptions.js

  1. /**
  2. * @fileoverview
  3. * This file is for exception and error handling in Kekule.
  4. * @author Partridge Jiang
  5. */
  6. /*
  7. * requires /lan/classes.js
  8. * requires /core/kekule.common.js
  9. */
  10. /**
  11. * @description Class for exception handler.
  12. * @class Kekule.ExceptionHandler
  13. * @augments ObjectEx
  14. */
  15. Kekule.ExceptionHandler = Class.create(ObjectEx,
  16. /** @lends Kekule.ExceptionHandler# */
  17. {
  18. /** @private */
  19. CLASS_NAME: 'Kekule.ExceptionHandler',
  20. /** @private */
  21. //instance: null,
  22. /** @private */
  23. initProperties: function()
  24. {
  25. //this.defineProp('enableDetail', {datatype: DataType.BOOL});
  26. /**
  27. * Invoked when an exception is throwed by ExceptionHandler
  28. * event param of it has field: {exception: Kekule.Exception}
  29. * If a listener modify the event param and add a new field e.stop = true,
  30. * then the exception will not thrown to JavaScript language system.
  31. * @name Kekule.ExceptionHandler#exceptionThrown
  32. * @event
  33. */
  34. this.defineEvent('exceptionThrown');
  35. },
  36. /** @private */
  37. notifyExceptionThrown: function(e, exceptionLevel)
  38. {
  39. var eventArg = {'exception': e, 'level': exceptionLevel};
  40. this.invokeEvent('exceptionThrown', eventArg);
  41. return eventArg;
  42. },
  43. /**
  44. * Throw an exception and invoke onExeption event
  45. * @param {Object} e Exception object.
  46. */
  47. throwException: function(e, exceptionLevel)
  48. {
  49. var EL = Kekule.ExceptionLevel;
  50. if (!exceptionLevel)
  51. exceptionLevel = EL.ERROR;
  52. var eventArg = this.notifyExceptionThrown(e, exceptionLevel || EL.ERROR);
  53. if (!eventArg.stop)
  54. {
  55. /*
  56. alert(e);
  57. throw e;
  58. */
  59. if ((!exceptionLevel) || (exceptionLevel === EL.ERROR))
  60. {
  61. var eo = e;
  62. if (typeof(e) === 'string')
  63. eo = new Kekule.Exception(e);
  64. throw eo;
  65. }
  66. else
  67. {
  68. if (typeof(console) !== 'undefined')
  69. {
  70. if (typeof(e) !== 'string')
  71. e = e.message;
  72. var method = (exceptionLevel === EL.WARNING)? 'warn':
  73. (exceptionLevel === EL.NOTE)? 'info':
  74. 'log';
  75. if (method)
  76. console[method](e);
  77. else
  78. console.log(e);
  79. }
  80. }
  81. }
  82. },
  83. /**
  84. * Throw an exception and invoke onExeption event, same as {@link Kekule.ExceptionHandler.throwException}
  85. */
  86. raise: function(e, exceptionLevel)
  87. {
  88. return this.throwException(e, exceptionLevel || Kekule.ExceptionLevel.ERROR);
  89. }
  90. });
  91. /** Get a singleton instance of Kekule.ExceptionHandler. */
  92. Kekule.ExceptionHandler.getInstance = function()
  93. {
  94. if (!this.instance)
  95. this.instance = new Kekule.ExceptionHandler();
  96. return this.instance;
  97. };
  98. Kekule.exceptionHandler = Kekule.ExceptionHandler.getInstance();
  99. /**
  100. * Base class for exception in Kekule
  101. * @class
  102. * @param {String} message Message of exception.
  103. * @param {String} name Name of exception.
  104. *
  105. * @property {String} message Message of exception, read only.
  106. */
  107. Kekule.Exception = function(message, name)
  108. {
  109. this.message = message;
  110. this.name = name;
  111. };
  112. Kekule.Exception.prototype = new Error();
  113. Kekule.Exception.prototype.constructor = Kekule.Exception;
  114. Object.extend(Kekule.Exception.prototype,
  115. {
  116. /** @lends Kekule.Exception# */
  117. getMessage: function()
  118. {
  119. return this.message;
  120. }
  121. }
  122. );
  123. /**
  124. * Class for a critical error.
  125. * @class
  126. * @augments Kekule.Exception
  127. * @param {String} message Message of exception.
  128. * @param {String} name Name of exception.
  129. */
  130. Kekule.Error = function(message, name)
  131. {
  132. this.message = message;
  133. this.name = name;
  134. };
  135. Kekule.Error.prototype = Kekule.Exception.prototype;
  136. Kekule.Error.prototype.constructor = Kekule.Error;
  137. /**
  138. * Class for a error about chemistry
  139. * @class
  140. * @augments Kekule.Error
  141. * @param {String} message Message of exception.
  142. * @param {String} name Name of exception.
  143. */
  144. Kekule.ChemError = function(message, name)
  145. {
  146. this.message = message;
  147. this.name = name;
  148. };
  149. Kekule.ChemError.prototype = Kekule.Error.prototype; // new Kekule.Error();
  150. Kekule.ChemError.prototype.constructor = Kekule.ChemError;
  151. // TODO: for debug only
  152. // If console is not defined, just add one to avoid exception in some browser
  153. if (typeof(console) == 'undefined')
  154. {
  155. console = {
  156. log: function() {},
  157. warn: function() {},
  158. info: function() {},
  159. error: function() {}
  160. };
  161. }