PageRenderTime 32ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/coffeeratings/public/coffeeratings-client/lib/extjs/src/lang/Error.js

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