PageRenderTime 76ms CodeModel.GetById 29ms app.highlight 9ms RepoModel.GetById 26ms app.codeStats 1ms

/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
 76    function toString() {
 77        var me = this,
 78            cls = me.sourceClass,
 79            method = me.sourceMethod,
 80            msg = me.msg;
 81
 82        if (method) {
 83            if (msg) {
 84                method += '(): ';
 85                method += msg;
 86            } else {
 87                method += '()';
 88            }
 89        }
 90
 91        if (cls) {
 92            method = method ? (cls + '.' + method) : cls;
 93        }
 94        
 95        return method || msg || '';
 96    }
 97
 98    Ext.Error = function(config) {
 99        if (Ext.isString(config)) {
100            config = { msg: config };
101        }
102
103        var error = new Error();
104
105        Ext.apply(error, config);
106
107        error.message = error.message || error.msg; // 'message' is standard ('msg' is non-standard)
108        // note: the above does not work in old WebKit (me.message is readonly) (Safari 4)
109
110        error.toString = toString;
111
112        return error;
113    };
114
115    Ext.apply(Ext.Error, {
116        /**
117         * @property {Boolean} ignore
118         * Static flag that can be used to globally disable error reporting to the browser if set to true
119         * (defaults to false). Note that if you ignore Ext errors it's likely that some other code may fail
120         * and throw a native JavaScript error thereafter, so use with caution. In most cases it will probably
121         * be preferable to supply a custom error {@link #handle handling} function instead.
122         *
123         * Example usage:
124         *
125         *     Ext.Error.ignore = true;
126         *
127         * @static
128         */
129        ignore: false,
130
131        /**
132         * Raise an error that can include additional data and supports automatic console logging if available.
133         * You can pass a string error message or an object with the `msg` attribute which will be used as the
134         * error message. The object can contain any other name-value attributes (or objects) to be logged
135         * along with the error.
136         *
137         * Note that after displaying the error message a JavaScript error will ultimately be thrown so that
138         * execution will halt.
139         *
140         * Example usage:
141         *
142         *     Ext.Error.raise('A simple string error message');
143         *
144         *     // or...
145         *
146         *     Ext.define('Ext.Foo', {
147         *         doSomething: function(option){
148         *             if (someCondition === false) {
149         *                 Ext.Error.raise({
150         *                     msg: 'You cannot do that!',
151         *                     option: option,   // whatever was passed into the method
152         *                     'error code': 100 // other arbitrary info
153         *                 });
154         *             }
155         *         }
156         *     });
157         *
158         * @param {String/Object} err The error message string, or an object containing the attribute "msg" that will be
159         * used as the error message. Any other data included in the object will also be logged to the browser console,
160         * if available.
161         * @static
162         */
163        raise: function(err) {
164            err = err || {};
165            if (Ext.isString(err)) {
166                err = { msg: err };
167            }
168
169            var me = this,
170                method = me.raise.caller,
171                msg, name;
172
173            if (method) {
174                if (!err.sourceMethod && (name = method.$name)) {
175                    err.sourceMethod = name;
176                }
177                if (!err.sourceClass && (name = method.$owner) && (name = name.$className)) {
178                    err.sourceClass = name;
179                }
180            }
181
182            if (me.handle(err) !== true) {
183                msg = toString.call(err);
184
185                //<debug>
186                Ext.log({
187                    msg: msg,
188                    level: 'error',
189                    dump: err,
190                    stack: true
191                });
192                //</debug>
193
194                throw new Ext.Error(err);
195            }
196        },
197
198        /**
199         * Globally handle any Ext errors that may be raised, optionally providing custom logic to
200         * handle different errors individually. Return true from the function to bypass throwing the
201         * error to the browser, otherwise the error will be thrown and execution will halt.
202         *
203         * Example usage:
204         *
205         *     Ext.Error.handle = function(err) {
206         *         if (err.someProperty == 'NotReallyAnError') {
207         *             // maybe log something to the application here if applicable
208         *             return true;
209         *         }
210         *         // any non-true return value (including none) will cause the error to be thrown
211         *     }
212         *
213         * @param {Object} err The error being raised. It will contain any attributes that were originally
214         * raised with it, plus properties about the method and class from which the error originated
215         * (if raised from a class that uses the Class System).
216         * @static
217         */
218        handle: function () {
219            return this.ignore;
220        }
221    });
222})();
223
224/*
225 * Create a function that will throw an error if called (in debug mode) with a message that
226 * indicates the method has been removed.
227 * @param {String} suggestion Optional text to include in the message (a workaround perhaps).
228 * @return {Function} The generated function.
229 * @private
230 */
231Ext.deprecated = function (suggestion) {
232    //<debug>
233    if (!suggestion) {
234        suggestion = '';
235    }
236
237    function fail () {
238        Ext.Error.raise('The method "' + fail.$owner.$className + '.' + fail.$name + 
239                '" has been removed. ' + suggestion);
240    }
241
242    return fail;
243    //</debug>
244    return Ext.emptyFn;
245};
246
247/*
248 * This mechanism is used to notify the user of the first error encountered on the page. In
249 * most cases errors go unobserved especially on IE. This mechanism pushes this information
250 * to the status bar so that users don't miss it.
251 */
252//<debug>
253(function () {
254    if (typeof window === 'undefined') {
255        return; // build system or some such environment...
256    }
257
258    var win = Ext.global,
259        last = 0,
260        // This method is called to notify the user of the current error status.
261        notify = function() {
262            var cnt = Ext.log && Ext.log.counters,
263                n = cnt && (cnt.error + cnt.warn + cnt.info + cnt.log),
264                msg;
265
266            // Put log counters to the status bar (for most browsers):
267            if (n && last !== n) {
268                msg = [];
269                if (cnt.error) {
270                    msg.push('Errors: ' + cnt.error);
271                }
272                if (cnt.warn) {
273                    msg.push('Warnings: ' + cnt.warn);
274                }
275                if (cnt.info) {
276                    msg.push('Info: ' + cnt.info);
277                }
278                if (cnt.log) {
279                    msg.push('Log: ' + cnt.log);
280                }
281                win.status = '*** ' + msg.join(' -- ');
282                last = n;
283            }
284        };
285
286    // window.onerror sounds ideal but it prevents the built-in error dialog from doing
287    // its (better) thing.
288    win.setInterval(notify, 1000);
289}());
290//</debug>