OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * This variable is checked in SelectFileDialogExtensionBrowserTest. | 6 * This variable is checked in SelectFileDialogExtensionBrowserTest. |
7 * @type {number} | 7 * @type {number} |
8 */ | 8 */ |
9 window.JSErrorCount = 0; | 9 window.JSErrorCount = 0; |
10 | 10 |
11 /** | 11 /** |
12 * Counts uncaught exceptions. | 12 * Counts uncaught exceptions. |
13 */ | 13 */ |
14 window.onerror = function() { window.JSErrorCount++; }; | 14 window.onerror = function() { window.JSErrorCount++; }; |
15 | 15 |
16 /** | 16 /** |
17 * Wraps the function to use it as a callback. | 17 * Wraps the function to use it as a callback. |
18 * This does: | 18 * This does: |
19 * - Capture the stack trace in case of error. | 19 * - Capture the stack trace in case of error. |
20 * - Bind this object | 20 * - Bind this object |
21 * | 21 * |
22 * @param {Object} thisObject Object to be used as this. | 22 * @param {Object=} opt_thisObject Object to be used as this. |
23 * @param {...} var_args Arguments to be bound with the wrapped function. | 23 * @param {...} var_args Arguments to be bound with the wrapped function. |
24 * @return {function} Wrapped function. | 24 * @return {function} Wrapped function. |
25 */ | 25 */ |
26 Function.prototype.wrap = function(thisObject, var_args) { | 26 Function.prototype.wrap = function(opt_thisObject, var_args) { |
27 var func = this; | 27 var func = this; |
28 var liveStack = (new Error('Stack trace before async call')).stack; | 28 var liveStack = (new Error('Stack trace before async call')).stack; |
29 if (thisObject === undefined) | 29 var thisObject = opt_thisObject || null; |
30 thisObject = null; | |
31 var boundArguments = Array.prototype.slice.call(arguments, 1); | 30 var boundArguments = Array.prototype.slice.call(arguments, 1); |
32 | 31 |
33 return function wrappedCallback(var_args) { | 32 return function wrappedCallback(var_args) { |
34 try { | 33 try { |
35 var args = boundArguments.concat(Array.prototype.slice.call(arguments)); | 34 var args = boundArguments.concat(Array.prototype.slice.call(arguments)); |
36 return func.apply(thisObject, args); | 35 return func.apply(thisObject, args); |
37 } catch (e) { | 36 } catch (e) { |
38 // Some async function doesn't handle exception correctly. So outputting | 37 // Some async function doesn't handle exception correctly. So outputting |
39 // the exception message and stack trace just in case. | 38 // the exception message and stack trace just in case. |
40 // The message will show twice if the caller handles exception correctly. | 39 // The message will show twice if the caller handles exception correctly. |
41 console.error(e.stack); | 40 console.error(e.stack); |
42 console.info('Exception above happened in callback.', liveStack); | 41 console.info('Exception above happened in callback.', liveStack); |
43 | 42 |
44 window.JSErrorCount++; | 43 window.JSErrorCount++; |
45 throw e; | 44 throw e; |
46 } | 45 } |
47 } | 46 } |
48 }; | 47 }; |
OLD | NEW |