PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Framework/ext-4.0.7/src/core/src/lang/Error.js

https://bitbucket.org/gtong/javascripts
JavaScript | 317 lines | 88 code | 25 blank | 204 comment | 22 complexity | 8dcee1f6ca046ffb6c988c4f03a4ba42 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  1. /*
  2. This file is part of Ext JS 4
  3. Copyright (c) 2011 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. Commercial Usage
  6. Licensees holding valid commercial licenses may use this file in accordance with the Commercial Software License Agreement provided with the Software or, alternatively, in accordance with the terms contained in a written agreement between you and Sencha.
  7. If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
  8. */
  9. /**
  10. * @author Brian Moeskau <brian@sencha.com>
  11. * @docauthor Brian Moeskau <brian@sencha.com>
  12. *
  13. * A wrapper class for the native JavaScript Error object that adds a few useful capabilities for handling
  14. * errors in an Ext application. When you use Ext.Error to {@link #raise} an error from within any class that
  15. * uses the Ext 4 class system, the Error class can automatically add the source class and method from which
  16. * the error was raised. It also includes logic to automatically log the eroor to the console, if available,
  17. * with additional metadata about the error. In all cases, the error will always be thrown at the end so that
  18. * execution will halt.
  19. *
  20. * Ext.Error also offers a global error {@link #handle handling} method that can be overridden in order to
  21. * handle application-wide errors in a single spot. You can optionally {@link #ignore} errors altogether,
  22. * although in a real application it's usually a better idea to override the handling function and perform
  23. * logging or some other method of reporting the errors in a way that is meaningful to the application.
  24. *
  25. * At its simplest you can simply raise an error as a simple string from within any code:
  26. *
  27. * Example usage:
  28. *
  29. * Ext.Error.raise('Something bad happened!');
  30. *
  31. * If raised from plain JavaScript code, the error will be logged to the console (if available) and the message
  32. * displayed. In most cases however you'll be raising errors from within a class, and it may often be useful to add
  33. * additional metadata about the error being raised. The {@link #raise} method can also take a config object.
  34. * In this form the `msg` attribute becomes the error description, and any other data added to the config gets
  35. * added to the error object and, if the console is available, logged to the console for inspection.
  36. *
  37. * Example usage:
  38. *
  39. * Ext.define('Ext.Foo', {
  40. * doSomething: function(option){
  41. * if (someCondition === false) {
  42. * Ext.Error.raise({
  43. * msg: 'You cannot do that!',
  44. * option: option, // whatever was passed into the method
  45. * 'error code': 100 // other arbitrary info
  46. * });
  47. * }
  48. * }
  49. * });
  50. *
  51. * If a console is available (that supports the `console.dir` function) you'll see console output like:
  52. *
  53. * An error was raised with the following data:
  54. * option: Object { foo: "bar"}
  55. * foo: "bar"
  56. * error code: 100
  57. * msg: "You cannot do that!"
  58. * sourceClass: "Ext.Foo"
  59. * sourceMethod: "doSomething"
  60. *
  61. * uncaught exception: You cannot do that!
  62. *
  63. * As you can see, the error will report exactly where it was raised and will include as much information as the
  64. * raising code can usefully provide.
  65. *
  66. * If you want to handle all application errors globally you can simply override the static {@link #handle} method
  67. * and provide whatever handling logic you need. If the method returns true then the error is considered handled
  68. * and will not be thrown to the browser. If anything but true is returned then the error will be thrown normally.
  69. *
  70. * Example usage:
  71. *
  72. * Ext.Error.handle = function(err) {
  73. * if (err.someProperty == 'NotReallyAnError') {
  74. * // maybe log something to the application here if applicable
  75. * return true;
  76. * }
  77. * // any non-true return value (including none) will cause the error to be thrown
  78. * }
  79. *
  80. */
  81. Ext.Error = Ext.extend(Error, {
  82. statics: {
  83. /**
  84. * @property {Boolean} ignore
  85. * Static flag that can be used to globally disable error reporting to the browser if set to true
  86. * (defaults to false). Note that if you ignore Ext errors it's likely that some other code may fail
  87. * and throw a native JavaScript error thereafter, so use with caution. In most cases it will probably
  88. * be preferable to supply a custom error {@link #handle handling} function instead.
  89. *
  90. * Example usage:
  91. *
  92. * Ext.Error.ignore = true;
  93. *
  94. * @static
  95. */
  96. ignore: false,
  97. /**
  98. * @property {Boolean} notify
  99. * Static flag that can be used to globally control error notification to the user. Unlike
  100. * Ex.Error.ignore, this does not effect exceptions. They are still thrown. This value can be
  101. * set to false to disable the alert notification (default is true for IE6 and IE7).
  102. *
  103. * Only the first error will generate an alert. Internally this flag is set to false when the
  104. * first error occurs prior to displaying the alert.
  105. *
  106. * This flag is not used in a release build.
  107. *
  108. * Example usage:
  109. *
  110. * Ext.Error.notify = false;
  111. *
  112. * @static
  113. */
  114. //notify: Ext.isIE6 || Ext.isIE7,
  115. /**
  116. * Raise an error that can include additional data and supports automatic console logging if available.
  117. * You can pass a string error message or an object with the `msg` attribute which will be used as the
  118. * error message. The object can contain any other name-value attributes (or objects) to be logged
  119. * along with the error.
  120. *
  121. * Note that after displaying the error message a JavaScript error will ultimately be thrown so that
  122. * execution will halt.
  123. *
  124. * Example usage:
  125. *
  126. * Ext.Error.raise('A simple string error message');
  127. *
  128. * // or...
  129. *
  130. * Ext.define('Ext.Foo', {
  131. * doSomething: function(option){
  132. * if (someCondition === false) {
  133. * Ext.Error.raise({
  134. * msg: 'You cannot do that!',
  135. * option: option, // whatever was passed into the method
  136. * 'error code': 100 // other arbitrary info
  137. * });
  138. * }
  139. * }
  140. * });
  141. *
  142. * @param {String/Object} err The error message string, or an object containing the attribute "msg" that will be
  143. * used as the error message. Any other data included in the object will also be logged to the browser console,
  144. * if available.
  145. * @static
  146. */
  147. raise: function(err){
  148. err = err || {};
  149. if (Ext.isString(err)) {
  150. err = { msg: err };
  151. }
  152. var method = this.raise.caller;
  153. if (method) {
  154. if (method.$name) {
  155. err.sourceMethod = method.$name;
  156. }
  157. if (method.$owner) {
  158. err.sourceClass = method.$owner.$className;
  159. }
  160. }
  161. if (Ext.Error.handle(err) !== true) {
  162. var msg = Ext.Error.prototype.toString.call(err);
  163. Ext.log({
  164. msg: msg,
  165. level: 'error',
  166. dump: err,
  167. stack: true
  168. });
  169. throw new Ext.Error(err);
  170. }
  171. },
  172. /**
  173. * Globally handle any Ext errors that may be raised, optionally providing custom logic to
  174. * handle different errors individually. Return true from the function to bypass throwing the
  175. * error to the browser, otherwise the error will be thrown and execution will halt.
  176. *
  177. * Example usage:
  178. *
  179. * Ext.Error.handle = function(err) {
  180. * if (err.someProperty == 'NotReallyAnError') {
  181. * // maybe log something to the application here if applicable
  182. * return true;
  183. * }
  184. * // any non-true return value (including none) will cause the error to be thrown
  185. * }
  186. *
  187. * @param {Ext.Error} err The Ext.Error object being raised. It will contain any attributes that were originally
  188. * raised with it, plus properties about the method and class from which the error originated (if raised from a
  189. * class that uses the Ext 4 class system).
  190. * @static
  191. */
  192. handle: function(){
  193. return Ext.Error.ignore;
  194. }
  195. },
  196. // This is the standard property that is the name of the constructor.
  197. name: 'Ext.Error',
  198. /**
  199. * Creates new Error object.
  200. * @param {String/Object} config The error message string, or an object containing the
  201. * attribute "msg" that will be used as the error message. Any other data included in
  202. * the object will be applied to the error instance and logged to the browser console, if available.
  203. */
  204. constructor: function(config){
  205. if (Ext.isString(config)) {
  206. config = { msg: config };
  207. }
  208. var me = this;
  209. Ext.apply(me, config);
  210. me.message = me.message || me.msg; // 'message' is standard ('msg' is non-standard)
  211. // note: the above does not work in old WebKit (me.message is readonly) (Safari 4)
  212. },
  213. /**
  214. * Provides a custom string representation of the error object. This is an override of the base JavaScript
  215. * `Object.toString` method, which is useful so that when logged to the browser console, an error object will
  216. * be displayed with a useful message instead of `[object Object]`, the default `toString` result.
  217. *
  218. * The default implementation will include the error message along with the raising class and method, if available,
  219. * but this can be overridden with a custom implementation either at the prototype level (for all errors) or on
  220. * a particular error instance, if you want to provide a custom description that will show up in the console.
  221. * @return {String} The error message. If raised from within the Ext 4 class system, the error message will also
  222. * include the raising class and method names, if available.
  223. */
  224. toString: function(){
  225. var me = this,
  226. className = me.className ? me.className : '',
  227. methodName = me.methodName ? '.' + me.methodName + '(): ' : '',
  228. msg = me.msg || '(No description provided)';
  229. return className + methodName + msg;
  230. }
  231. });
  232. /*
  233. * This mechanism is used to notify the user of the first error encountered on the page. This
  234. * was previously internal to Ext.Error.raise and is a desirable feature since errors often
  235. * slip silently under the radar. It cannot live in Ext.Error.raise since there are times
  236. * where exceptions are handled in a try/catch.
  237. */
  238. //<debug>
  239. (function () {
  240. var prevOnError, timer, errors = 0,
  241. extraordinarilyBad = /(out of stack)|(too much recursion)|(stack overflow)|(out of memory)/i,
  242. win = Ext.global;
  243. if (typeof window === 'undefined') {
  244. return; // build system or some such environment...
  245. }
  246. // This method is called to notify the user of the current error status.
  247. function notify () {
  248. var counters = Ext.log.counters,
  249. supports = Ext.supports,
  250. hasOnError = supports && supports.WindowOnError; // TODO - timing
  251. // Put log counters to the status bar (for most browsers):
  252. if (counters && (counters.error + counters.warn + counters.info + counters.log)) {
  253. var msg = [ 'Logged Errors:',counters.error, 'Warnings:',counters.warn,
  254. 'Info:',counters.info, 'Log:',counters.log].join(' ');
  255. if (errors) {
  256. msg = '*** Errors: ' + errors + ' - ' + msg;
  257. } else if (counters.error) {
  258. msg = '*** ' + msg;
  259. }
  260. win.status = msg;
  261. }
  262. // Display an alert on the first error:
  263. if (!Ext.isDefined(Ext.Error.notify)) {
  264. Ext.Error.notify = Ext.isIE6 || Ext.isIE7; // TODO - timing
  265. }
  266. if (Ext.Error.notify && (hasOnError ? errors : (counters && counters.error))) {
  267. Ext.Error.notify = false;
  268. if (timer) {
  269. win.clearInterval(timer); // ticks can queue up so stop...
  270. timer = null;
  271. }
  272. alert('Unhandled error on page: See console or log');
  273. poll();
  274. }
  275. }
  276. // Sets up polling loop. This is the only way to know about errors in some browsers
  277. // (Opera/Safari) and is the only way to update the status bar for warnings and other
  278. // non-errors.
  279. function poll () {
  280. timer = win.setInterval(notify, 1000);
  281. }
  282. // window.onerror sounds ideal but it prevents the built-in error dialog from doing
  283. // its (better) thing.
  284. poll();
  285. })();
  286. //</debug>