4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-Error'>/**
19 </span> * @class Ext.Error
23 A wrapper class for the native JavaScript Error object that adds a few useful capabilities for handling
24 errors in an Ext application. When you use Ext.Error to {@link #raise} an error from within any class that
25 uses the Ext 4 class system, the Error class can automatically add the source class and method from which
26 the error was raised. It also includes logic to automatically log the eroor to the console, if available,
27 with additional metadata about the error. In all cases, the error will always be thrown at the end so that
30 Ext.Error also offers a global error {@link #handle handling} method that can be overridden in order to
31 handle application-wide errors in a single spot. You can optionally {@link #ignore} errors altogether,
32 although in a real application it's usually a better idea to override the handling function and perform
33 logging or some other method of reporting the errors in a way that is meaningful to the application.
35 At its simplest you can simply raise an error as a simple string from within any code:
39 Ext.Error.raise('Something bad happened!');
41 If raised from plain JavaScript code, the error will be logged to the console (if available) and the message
42 displayed. In most cases however you'll be raising errors from within a class, and it may often be useful to add
43 additional metadata about the error being raised. The {@link #raise} method can also take a config object.
44 In this form the `msg` attribute becomes the error description, and any other data added to the config gets
45 added to the error object and, if the console is available, logged to the console for inspection.
49 Ext.define('Ext.Foo', {
50 doSomething: function(option){
51 if (someCondition === false) {
53 msg: 'You cannot do that!',
54 option: option, // whatever was passed into the method
55 'error code': 100 // other arbitrary info
61 If a console is available (that supports the `console.dir` function) you'll see console output like:
63 An error was raised with the following data:
64 option: Object { foo: "bar"}
67 msg: "You cannot do that!"
68 sourceClass: "Ext.Foo"
69 sourceMethod: "doSomething"
71 uncaught exception: You cannot do that!
73 As you can see, the error will report exactly where it was raised and will include as much information as the
74 raising code can usefully provide.
76 If you want to handle all application errors globally you can simply override the static {@link #handle} method
77 and provide whatever handling logic you need. If the method returns true then the error is considered handled
78 and will not be thrown to the browser. If anything but true is returned then the error will be thrown normally.
82 Ext.Error.handle = function(err) {
83 if (err.someProperty == 'NotReallyAnError') {
84 // maybe log something to the application here if applicable
87 // any non-true return value (including none) will cause the error to be thrown
90 * Create a new Error object
91 * @param {Object} config The config object
93 * @author Brian Moeskau <brian@sencha.com>
94 * @docauthor Brian Moeskau <brian@sencha.com>
96 Ext.Error = Ext.extend(Error, {
98 <span id='Ext-Error-property-ignore'> /**
99 </span> * @property ignore
100 Static flag that can be used to globally disable error reporting to the browser if set to true
101 (defaults to false). Note that if you ignore Ext errors it's likely that some other code may fail
102 and throw a native JavaScript error thereafter, so use with caution. In most cases it will probably
103 be preferable to supply a custom error {@link #handle handling} function instead.
107 Ext.Error.ignore = true;
114 <span id='Ext-Error-property-notify'> /**
115 </span> * @property notify
116 Static flag that can be used to globally control error notification to the user. Unlike
117 Ex.Error.ignore, this does not effect exceptions. They are still thrown. This value can be
118 set to false to disable the alert notification (default is true for IE6 and IE7).
120 Only the first error will generate an alert. Internally this flag is set to false when the
121 first error occurs prior to displaying the alert.
123 This flag is not used in a release build.
127 Ext.Error.notify = false;
132 //notify: Ext.isIE6 || Ext.isIE7,
134 <span id='Ext-Error-method-raise'> /**
135 </span>Raise an error that can include additional data and supports automatic console logging if available.
136 You can pass a string error message or an object with the `msg` attribute which will be used as the
137 error message. The object can contain any other name-value attributes (or objects) to be logged
138 along with the error.
140 Note that after displaying the error message a JavaScript error will ultimately be thrown so that
145 Ext.Error.raise('A simple string error message');
149 Ext.define('Ext.Foo', {
150 doSomething: function(option){
151 if (someCondition === false) {
153 msg: 'You cannot do that!',
154 option: option, // whatever was passed into the method
155 'error code': 100 // other arbitrary info
160 * @param {String/Object} err The error message string, or an object containing the
161 * attribute "msg" that will be used as the error message. Any other data included in
162 * the object will also be logged to the browser console, if available.
166 raise: function(err){
168 if (Ext.isString(err)) {
172 var method = this.raise.caller;
176 err.sourceMethod = method.$name;
179 err.sourceClass = method.$owner.$className;
183 if (Ext.Error.handle(err) !== true) {
184 var msg = Ext.Error.prototype.toString.call(err);
193 throw new Ext.Error(err);
197 <span id='Ext-Error-method-handle'> /**
198 </span>Globally handle any Ext errors that may be raised, optionally providing custom logic to
199 handle different errors individually. Return true from the function to bypass throwing the
200 error to the browser, otherwise the error will be thrown and execution will halt.
204 Ext.Error.handle = function(err) {
205 if (err.someProperty == 'NotReallyAnError') {
206 // maybe log something to the application here if applicable
209 // any non-true return value (including none) will cause the error to be thrown
212 * @param {Ext.Error} err The Ext.Error object being raised. It will contain any attributes
213 * that were originally raised with it, plus properties about the method and class from which
214 * the error originated (if raised from a class that uses the Ext 4 class system).
219 return Ext.Error.ignore;
223 // This is the standard property that is the name of the constructor.
226 <span id='Ext-Error-method-constructor'> /**
227 </span> * @param {String/Object} config The error message string, or an object containing the
228 * attribute "msg" that will be used as the error message. Any other data included in
229 * the object will be applied to the error instance and logged to the browser console, if available.
231 constructor: function(config){
232 if (Ext.isString(config)) {
233 config = { msg: config };
238 Ext.apply(me, config);
240 me.message = me.message || me.msg; // 'message' is standard ('msg' is non-standard)
241 // note: the above does not work in old WebKit (me.message is readonly) (Safari 4)
244 <span id='Ext-Error-method-toString'> /**
245 </span>Provides a custom string representation of the error object. This is an override of the base JavaScript
246 `Object.toString` method, which is useful so that when logged to the browser console, an error object will
247 be displayed with a useful message instead of `[object Object]`, the default `toString` result.
249 The default implementation will include the error message along with the raising class and method, if available,
250 but this can be overridden with a custom implementation either at the prototype level (for all errors) or on
251 a particular error instance, if you want to provide a custom description that will show up in the console.
253 * @return {String} The error message. If raised from within the Ext 4 class system, the error message
254 * will also include the raising class and method names, if available.
256 toString: function(){
258 className = me.className ? me.className : '',
259 methodName = me.methodName ? '.' + me.methodName + '(): ' : '',
260 msg = me.msg || '(No description provided)';
262 return className + methodName + msg;
267 * This mechanism is used to notify the user of the first error encountered on the page. This
268 * was previously internal to Ext.Error.raise and is a desirable feature since errors often
269 * slip silently under the radar. It cannot live in Ext.Error.raise since there are times
270 * where exceptions are handled in a try/catch.
274 var prevOnError, timer, errors = 0,
275 extraordinarilyBad = /(out of stack)|(too much recursion)|(stack overflow)|(out of memory)/i,
278 if (typeof window === 'undefined') {
279 return; // build system or some such environment...
282 // This method is called to notify the user of the current error status.
284 var counters = Ext.log.counters,
285 supports = Ext.supports,
286 hasOnError = supports && supports.WindowOnError; // TODO - timing
288 // Put log counters to the status bar (for most browsers):
289 if (counters && (counters.error + counters.warn + counters.info + counters.log)) {
290 var msg = [ 'Logged Errors:',counters.error, 'Warnings:',counters.warn,
291 'Info:',counters.info, 'Log:',counters.log].join(' ');
293 msg = '*** Errors: ' + errors + ' - ' + msg;
294 } else if (counters.error) {
300 // Display an alert on the first error:
301 if (!Ext.isDefined(Ext.Error.notify)) {
302 Ext.Error.notify = Ext.isIE6 || Ext.isIE7; // TODO - timing
304 if (Ext.Error.notify && (hasOnError ? errors : (counters && counters.error))) {
305 Ext.Error.notify = false;
308 win.clearInterval(timer); // ticks can queue up so stop...
312 alert('Unhandled error on page: See console or log');
317 // Sets up polling loop. This is the only way to know about errors in some browsers
318 // (Opera/Safari) and is the only way to update the status bar for warnings and other
321 timer = win.setInterval(notify, 1000);
324 // window.onerror is ideal (esp in IE) because you get full context. This is harmless
325 // otherwise (never called) which is good because you cannot feature detect it.
326 prevOnError = win.onerror || Ext.emptyFn;
327 win.onerror = function (message) {
330 if (!extraordinarilyBad.test(message)) {
331 // too much recursion + our alert right now = crash IE
332 // our polling loop will pick it up even if we don't alert now
336 return prevOnError.apply(this, arguments);