PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/archesproject/arches/Media/js/ui_frameworks/ext-4.0.2a/src/core/src/lang/Error.js

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