Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / file_manager / file_manager / common / js / error_util.js
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 /**
6  * This variable is checked in SelectFileDialogExtensionBrowserTest.
7  * @type {number}
8  */
9 window.JSErrorCount = 0;
10
11 /**
12  * Count uncaught exceptions.
13  */
14 window.onerror = function() { window.JSErrorCount++; };
15
16 // Overrides console.error() to count errors.
17 /**
18  * @param {...*} var_args Message to be logged.
19  */
20 console.error = (function() {
21   var orig = console.error;
22   return function() {
23     window.JSErrorCount++;
24     return orig.apply(this, arguments);
25   };
26 })();
27
28 // Overrides console.assert() to count errors.
29 /**
30  * @param {boolean} condition If false, log a message and stack trace.
31  * @param {...*} var_args Objects to.
32  */
33 console.assert = (function() {
34   var orig = console.assert;
35   return function(condition) {
36     if (!condition)
37       window.JSErrorCount++;
38     return orig.apply(this, arguments);
39   };
40 })();
41
42 /**
43  * Wraps the function to use it as a callback.
44  * This does:
45  *  - Capture the stack trace in case of error.
46  *  - Bind this object
47  *
48  * @param {Object} thisObject Object to be used as this.
49  * @return {Function} Wrapped function.
50  */
51 Function.prototype.wrap = function(thisObject) {
52   var func = this;
53   var liveStack = (new Error('Stack trace before async call')).stack;
54   if (thisObject === undefined)
55     thisObject = null;
56
57   return function wrappedCallback() {
58     try {
59       return func.apply(thisObject, arguments);
60     } catch (e) {
61       console.error('Exception happens in callback.', liveStack);
62
63       window.JSErrorCount++;
64       throw e;
65     }
66   }
67 };