PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/web-app/client/ext/packages/sencha-core/src/lang/Error.js

https://github.com/mxrguspxrt/klassifikaator
JavaScript | 290 lines | 104 code | 25 blank | 161 comment | 27 complexity | 68f9c63e6d263f4039bb3063a5c17de5 MD5 | raw file
  1. /**
  2. * A helper class for the native JavaScript Error object that adds a few useful capabilities for handling
  3. * errors in an application. When you use Ext.Error to {@link #raise} an error from within any class that
  4. * uses the Class System, the Error class can automatically add the source class and method from which
  5. * the error was raised. It also includes logic to automatically log the error to the console, if available,
  6. * with additional metadata about the error. In all cases, the error will always be thrown at the end so that
  7. * execution will halt.
  8. *
  9. * Ext.Error also offers a global error {@link #handle handling} method that can be overridden in order to
  10. * handle application-wide errors in a single spot. You can optionally {@link #ignore} errors altogether,
  11. * although in a real application it's usually a better idea to override the handling function and perform
  12. * logging or some other method of reporting the errors in a way that is meaningful to the application.
  13. *
  14. * At its simplest you can simply raise an error as a simple string from within any code:
  15. *
  16. * Example usage:
  17. *
  18. * Ext.Error.raise('Something bad happened!');
  19. *
  20. * If raised from plain JavaScript code, the error will be logged to the console (if available) and the message
  21. * displayed. In most cases however you'll be raising errors from within a class, and it may often be useful to add
  22. * additional metadata about the error being raised. The {@link #raise} method can also take a config object.
  23. * In this form the `msg` attribute becomes the error description, and any other data added to the config gets
  24. * added to the error object and, if the console is available, logged to the console for inspection.
  25. *
  26. * Example usage:
  27. *
  28. * Ext.define('Ext.Foo', {
  29. * doSomething: function(option){
  30. * if (someCondition === false) {
  31. * Ext.Error.raise({
  32. * msg: 'You cannot do that!',
  33. * option: option, // whatever was passed into the method
  34. * 'error code': 100 // other arbitrary info
  35. * });
  36. * }
  37. * }
  38. * });
  39. *
  40. * If a console is available (that supports the `console.dir` function) you'll see console output like:
  41. *
  42. * An error was raised with the following data:
  43. * option: Object { foo: "bar"}
  44. * foo: "bar"
  45. * error code: 100
  46. * msg: "You cannot do that!"
  47. * sourceClass: "Ext.Foo"
  48. * sourceMethod: "doSomething"
  49. *
  50. * uncaught exception: You cannot do that!
  51. *
  52. * As you can see, the error will report exactly where it was raised and will include as much information as the
  53. * raising code can usefully provide.
  54. *
  55. * If you want to handle all application errors globally you can simply override the static {@link #handle} method
  56. * and provide whatever handling logic you need. If the method returns true then the error is considered handled
  57. * and will not be thrown to the browser. If anything but true is returned then the error will be thrown normally.
  58. *
  59. * Example usage:
  60. *
  61. * Ext.Error.handle = function(err) {
  62. * if (err.someProperty == 'NotReallyAnError') {
  63. * // maybe log something to the application here if applicable
  64. * return true;
  65. * }
  66. * // any non-true return value (including none) will cause the error to be thrown
  67. * }
  68. *
  69. * @class Ext.Error
  70. */
  71. (function() {
  72. // @define Ext.lang.Error
  73. // @define Ext.Error
  74. // @require Ext
  75. function toString() {
  76. var me = this,
  77. cls = me.sourceClass,
  78. method = me.sourceMethod,
  79. msg = me.msg;
  80. if (method) {
  81. if (msg) {
  82. method += '(): ';
  83. method += msg;
  84. } else {
  85. method += '()';
  86. }
  87. }
  88. if (cls) {
  89. method = method ? (cls + '.' + method) : cls;
  90. }
  91. return method || msg || '';
  92. }
  93. Ext.Error = function(config) {
  94. if (Ext.isString(config)) {
  95. config = { msg: config };
  96. }
  97. var error = new Error();
  98. Ext.apply(error, config);
  99. error.message = error.message || error.msg; // 'message' is standard ('msg' is non-standard)
  100. // note: the above does not work in old WebKit (me.message is readonly) (Safari 4)
  101. error.toString = toString;
  102. return error;
  103. };
  104. Ext.apply(Ext.Error, {
  105. /**
  106. * @property {Boolean} ignore
  107. * Static flag that can be used to globally disable error reporting to the browser if set to true
  108. * (defaults to false). Note that if you ignore Ext errors it's likely that some other code may fail
  109. * and throw a native JavaScript error thereafter, so use with caution. In most cases it will probably
  110. * be preferable to supply a custom error {@link #handle handling} function instead.
  111. *
  112. * Example usage:
  113. *
  114. * Ext.Error.ignore = true;
  115. *
  116. * @static
  117. */
  118. ignore: false,
  119. /**
  120. * Raise an error that can include additional data and supports automatic console logging if available.
  121. * You can pass a string error message or an object with the `msg` attribute which will be used as the
  122. * error message. The object can contain any other name-value attributes (or objects) to be logged
  123. * along with the error.
  124. *
  125. * Note that after displaying the error message a JavaScript error will ultimately be thrown so that
  126. * execution will halt.
  127. *
  128. * Example usage:
  129. *
  130. * Ext.Error.raise('A simple string error message');
  131. *
  132. * // or...
  133. *
  134. * Ext.define('Ext.Foo', {
  135. * doSomething: function(option){
  136. * if (someCondition === false) {
  137. * Ext.Error.raise({
  138. * msg: 'You cannot do that!',
  139. * option: option, // whatever was passed into the method
  140. * 'error code': 100 // other arbitrary info
  141. * });
  142. * }
  143. * }
  144. * });
  145. *
  146. * @param {String/Object} err The error message string, or an object containing the attribute "msg" that will be
  147. * used as the error message. Any other data included in the object will also be logged to the browser console,
  148. * if available.
  149. * @static
  150. */
  151. raise: function(err) {
  152. err = err || {};
  153. if (Ext.isString(err)) {
  154. err = { msg: err };
  155. }
  156. var me = this,
  157. method = me.raise.caller,
  158. msg, name;
  159. if (method) {
  160. if (!err.sourceMethod && (name = method.$name)) {
  161. err.sourceMethod = name;
  162. }
  163. if (!err.sourceClass && (name = method.$owner) && (name = name.$className)) {
  164. err.sourceClass = name;
  165. }
  166. }
  167. if (me.handle(err) !== true) {
  168. msg = toString.call(err);
  169. //<debug>
  170. Ext.log({
  171. msg: msg,
  172. level: 'error',
  173. dump: err,
  174. stack: true
  175. });
  176. //</debug>
  177. throw new Ext.Error(err);
  178. }
  179. },
  180. /**
  181. * Globally handle any Ext errors that may be raised, optionally providing custom logic to
  182. * handle different errors individually. Return true from the function to bypass throwing the
  183. * error to the browser, otherwise the error will be thrown and execution will halt.
  184. *
  185. * Example usage:
  186. *
  187. * Ext.Error.handle = function(err) {
  188. * if (err.someProperty == 'NotReallyAnError') {
  189. * // maybe log something to the application here if applicable
  190. * return true;
  191. * }
  192. * // any non-true return value (including none) will cause the error to be thrown
  193. * }
  194. *
  195. * @param {Object} err The error being raised. It will contain any attributes that were originally
  196. * raised with it, plus properties about the method and class from which the error originated
  197. * (if raised from a class that uses the Class System).
  198. * @static
  199. */
  200. handle: function () {
  201. return this.ignore;
  202. }
  203. });
  204. })();
  205. /*
  206. * Create a function that will throw an error if called (in debug mode) with a message that
  207. * indicates the method has been removed.
  208. * @param {String} suggestion Optional text to include in the message (a workaround perhaps).
  209. * @return {Function} The generated function.
  210. * @private
  211. */
  212. Ext.deprecated = function (suggestion) {
  213. //<debug>
  214. if (!suggestion) {
  215. suggestion = '';
  216. }
  217. function fail () {
  218. Ext.Error.raise('The method "' + fail.$owner.$className + '.' + fail.$name +
  219. '" has been removed. ' + suggestion);
  220. }
  221. return fail;
  222. //</debug>
  223. return Ext.emptyFn;
  224. };
  225. /*
  226. * This mechanism is used to notify the user of the first error encountered on the page. In
  227. * most cases errors go unobserved especially on IE. This mechanism pushes this information
  228. * to the status bar so that users don't miss it.
  229. */
  230. //<debug>
  231. (function () {
  232. if (typeof window === 'undefined') {
  233. return; // build system or some such environment...
  234. }
  235. var win = Ext.global,
  236. last = 0,
  237. // This method is called to notify the user of the current error status.
  238. notify = function() {
  239. var cnt = Ext.log && Ext.log.counters,
  240. n = cnt && (cnt.error + cnt.warn + cnt.info + cnt.log),
  241. msg;
  242. // Put log counters to the status bar (for most browsers):
  243. if (n && last !== n) {
  244. msg = [];
  245. if (cnt.error) {
  246. msg.push('Errors: ' + cnt.error);
  247. }
  248. if (cnt.warn) {
  249. msg.push('Warnings: ' + cnt.warn);
  250. }
  251. if (cnt.info) {
  252. msg.push('Info: ' + cnt.info);
  253. }
  254. if (cnt.log) {
  255. msg.push('Log: ' + cnt.log);
  256. }
  257. win.status = '*** ' + msg.join(' -- ');
  258. last = n;
  259. }
  260. };
  261. // window.onerror sounds ideal but it prevents the built-in error dialog from doing
  262. // its (better) thing.
  263. win.setInterval(notify, 1000);
  264. }());
  265. //</debug>