Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(141)

Side by Side Diff: chrome/browser/resources/google_now/utility.js

Issue 22518002: Incremental changes in error reporting (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Better telling runtime errors from non-runtime ones. Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 'use strict'; 5 'use strict';
6 6
7 /** 7 /**
8 * @fileoverview Utility objects and functions for Google Now extension. 8 * @fileoverview Utility objects and functions for Google Now extension.
9 */ 9 */
10 10
11 // TODO(vadimt): Figure out the server name. Use it in the manifest and for 11 // TODO(vadimt): Figure out the server name. Use it in the manifest and for
12 // NOTIFICATION_CARDS_URL. Meanwhile, to use the feature, you need to manually 12 // NOTIFICATION_CARDS_URL. Meanwhile, to use the feature, you need to manually
13 // set the server name via local storage. 13 // set the server name via local storage.
14 14
15 /** 15 /**
16 * Notification server URL. 16 * Notification server URL.
17 */ 17 */
18 var NOTIFICATION_CARDS_URL = localStorage['server_url']; 18 var NOTIFICATION_CARDS_URL = localStorage['server_url'];
19 19
20 var DEBUG_MODE = localStorage['debug_mode']; 20 var DEBUG_MODE = localStorage['debug_mode'];
21 21
22 /** 22 /**
23 * Shows a message popup in debug mode. 23 * Builds an error object.
24 * @param {string} message Diagnostic message. 24 * @param {string} message Error message.
25 * @return {Error} Error object.
25 */ 26 */
26 function debugAlert(message) { 27 function buildError(message) {
rgustafson 2013/08/08 20:14:01 buildNonRuntimeError? buildCustomError? This is mo
vadimt 2013/08/08 20:25:28 You cannot build a runtime error. And 'custom' is
skare_ 2013/08/09 23:02:18 agree it might be nice to differentiate somehow; '
rgustafson 2013/08/09 23:08:22 Yeah, I still think this needs a modifier. I don't
vadimt 2013/08/10 00:45:27 Done.
vadimt 2013/08/10 00:45:27 Done.
27 if (DEBUG_MODE) 28 var error = new Error(message);
28 alert(message); 29 error.notFromRuntime = true;
30 return error;
29 } 31 }
30 32
31 /** 33 /**
32 * Checks for internal errors. 34 * Checks for internal errors.
33 * @param {boolean} condition Condition that must be true. 35 * @param {boolean} condition Condition that must be true.
34 * @param {string} message Diagnostic message for the case when the condition is 36 * @param {string} message Diagnostic message for the case when the condition is
35 * false. 37 * false.
36 */ 38 */
37 function verify(condition, message) { 39 function verify(condition, message) {
38 if (!condition) 40 if (!condition)
39 throw new Error('\nASSERT: ' + message); 41 throw buildError('ASSERT: ' + message);
40 } 42 }
41 43
42 /** 44 /**
43 * Builds a request to the notification server. 45 * Builds a request to the notification server.
44 * @param {string} handlerName Server handler to send the request to. 46 * @param {string} handlerName Server handler to send the request to.
45 * @param {string} contentType Value for the Content-type header. 47 * @param {string} contentType Value for the Content-type header.
46 * @return {XMLHttpRequest} Server request. 48 * @return {XMLHttpRequest} Server request.
47 */ 49 */
48 function buildServerRequest(handlerName, contentType) { 50 function buildServerRequest(handlerName, contentType) {
49 var request = new XMLHttpRequest(); 51 var request = new XMLHttpRequest();
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 /** 96 /**
95 * True if currently executed code runs in an instrumented callback. 97 * True if currently executed code runs in an instrumented callback.
96 * @type {boolean} 98 * @type {boolean}
97 */ 99 */
98 var isInInstrumentedCallback = false; 100 var isInInstrumentedCallback = false;
99 101
100 /** 102 /**
101 * Checks that we run in an instrumented callback. 103 * Checks that we run in an instrumented callback.
102 */ 104 */
103 function checkInInstrumentedCallback() { 105 function checkInInstrumentedCallback() {
104 if (!isInInstrumentedCallback) { 106 if (!isInInstrumentedCallback)
105 // Cannot use verify() since no one will catch the exception. 107 reportError(buildError('Not in instrumented callback'));
106 // This check will detect bugs at the development stage, and is very
107 // unlikely to be seen by users.
108 var error = 'Not in instrumented callback: ' + new Error().stack;
109 console.error(error);
110 debugAlert(error);
111 }
112 } 108 }
113 109
114 /** 110 /**
115 * Starts the first queued task. 111 * Starts the first queued task.
116 */ 112 */
117 function startFirst() { 113 function startFirst() {
118 verify(queue.length >= 1, 'startFirst: queue is empty'); 114 verify(queue.length >= 1, 'startFirst: queue is empty');
119 verify(!isInTask, 'startFirst: already in task'); 115 verify(!isInTask, 'startFirst: already in task');
120 isInTask = true; 116 isInTask = true;
121 117
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 182
187 if (queue.length >= 1) 183 if (queue.length >= 1)
188 startFirst(); 184 startFirst();
189 } 185 }
190 186
191 // Limiting 1 error report per background page load. 187 // Limiting 1 error report per background page load.
192 var errorReported = false; 188 var errorReported = false;
193 189
194 /** 190 /**
195 * Sends an error report to the server. 191 * Sends an error report to the server.
196 * @param {Error} error Error to report. 192 * @param {Error} error Error to send.
197 */ 193 */
198 function sendErrorReport(error) { 194 function sendErrorReport(error) {
199 var filteredStack = error.stack.replace(/.*\n/, '\n'); 195 var filteredStack = error.notFromRuntime ?
skare_ 2013/08/09 23:02:18 comment here could help future readers.
vadimt 2013/08/10 00:45:27 New name is self-explaining.
196 error.stack : error.stack.replace(/.*\n/, 'JS runtime error\n');
200 var file; 197 var file;
201 var line; 198 var line;
202 var topFrameMatches = filteredStack.match(/\(.*\)/); 199 var topFrameLineMatch = filteredStack.match(/\n at .*\n/);
203 // topFrameMatches's example: 200 var topFrame = topFrameLineMatch && topFrameLineMatch[0];
204 // (chrome-extension://pmofbkohncoogjjhahejjfbppikbjigm/utility.js:308:19) 201 if (topFrame) {
205 var crashLocation = topFrameMatches && topFrameMatches[0]; 202 // Examples of a frame:
206 if (crashLocation) { 203 // 1. '\n at someFunction (chrome-extension://
207 var topFrameElements = 204 // pmofbkohncoogjjhahejjfbppikbjigm/background.js:915:15)\n'
208 crashLocation.substring(1, crashLocation.length - 1).split(':'); 205 // 2. '\n at chrome-extension://pmofbkohncoogjjhahejjfbppikbjigm/
209 // topFrameElements for the above example will look like: 206 // utility.js:269:18\n'
210 // [0] chrome-extension 207 // 3. '\n at Function.target.(anonymous function) (extensions::
211 // [1] //pmofbkohncoogjjhahejjfbppikbjigm/utility.js 208 // SafeBuiltins:19:14)\n'
212 // [2] 308 209 // 4. '\n at Event.dispatchToListener (event_bindings:382:22)\n'
213 // [3] 19 210 var errorLocation;
211 // Find the the parentheses at the end of the line, if any.
212 var parenthesesMatch = topFrame.match(/\(.*\)\n/);
213 if (parenthesesMatch && parenthesesMatch[0]) {
214 errorLocation =
215 parenthesesMatch[0].substring(1, parenthesesMatch[0].length - 2);
216 } else {
217 errorLocation = topFrame;
rgustafson 2013/08/08 20:14:01 Have you seen this happen? I don't know if attempt
vadimt 2013/08/08 20:25:28 Sure, this example (2.) with anonymous top frame.
218 }
219
220 var topFrameElements = errorLocation.split(':');
221 // topFrameElements is an array that ends like:
222 // [N-3] //pmofbkohncoogjjhahejjfbppikbjigm/utility.js
223 // [N-2] 308
224 // [N-1] 19
214 if (topFrameElements.length >= 3) { 225 if (topFrameElements.length >= 3) {
215 file = topFrameElements[0] + ':' + topFrameElements[1]; 226 file = topFrameElements[topFrameElements.length - 3];
rgustafson 2013/08/08 20:14:01 Seems weird to just drop the chrome-extension.
vadimt 2013/08/08 20:25:28 That's not a big deal, but this way, we support al
rgustafson 2013/08/09 23:08:22 Okay, that's fine.
216 line = topFrameElements[2]; 227 line = topFrameElements[topFrameElements.length - 2];
217 } 228 }
218 } 229 }
230
219 var requestParameters = 231 var requestParameters =
220 'error=' + encodeURIComponent(error.name) + 232 'error=' + encodeURIComponent(error.name) +
221 '&script=' + encodeURIComponent(file) + 233 '&script=' + encodeURIComponent(file) +
222 '&line=' + encodeURIComponent(line) + 234 '&line=' + encodeURIComponent(line) +
223 '&trace=' + encodeURIComponent(filteredStack); 235 '&trace=' + encodeURIComponent(filteredStack);
224 var request = buildServerRequest('jserror', 236 var request = buildServerRequest('jserror',
225 'application/x-www-form-urlencoded'); 237 'application/x-www-form-urlencoded');
226 request.onloadend = function(event) { 238 request.onloadend = function(event) {
227 console.log('sendErrorReport status: ' + request.status); 239 console.log('sendErrorReport status: ' + request.status);
228 }; 240 };
229 request.send(requestParameters); 241 request.send(requestParameters);
230 } 242 }
231 243
232 /** 244 /**
245 * Reports an error to the server and the user, as appropriate.
246 * @param {Error} error Error to report.
247 */
248 function reportError(error) {
249 var message = 'Critical error:\n' + error.stack;
250 console.error(message);
251 if (!errorReported) {
252 errorReported = true;
253 chrome.metricsPrivate.getIsCrashReportingEnabled(function(isEnabled) {
254 if (isEnabled)
255 sendErrorReport(error);
256 if (DEBUG_MODE)
257 alert(message);
258 });
259 }
260 }
261
262 /**
233 * Unique ID of the next callback. 263 * Unique ID of the next callback.
234 * @type {number} 264 * @type {number}
235 */ 265 */
236 var nextCallbackId = 0; 266 var nextCallbackId = 0;
237 267
238 /** 268 /**
239 * Adds error processing to an API callback. 269 * Adds error processing to an API callback.
240 * @param {Function} callback Callback to instrument. 270 * @param {Function} callback Callback to instrument.
241 * @param {boolean=} opt_isEventListener True if the callback is an event 271 * @param {boolean=} opt_isEventListener True if the callback is an event
242 * listener. 272 * listener.
(...skipping 29 matching lines...) Expand all
272 'Instrumented callback is not instrumented upon exit'); 302 'Instrumented callback is not instrumented upon exit');
273 isInInstrumentedCallback = false; 303 isInInstrumentedCallback = false;
274 304
275 if (isTaskCallback) { 305 if (isTaskCallback) {
276 verify(isInTask, 'wrapCallback: not in task at exit'); 306 verify(isInTask, 'wrapCallback: not in task at exit');
277 isInTask = false; 307 isInTask = false;
278 if (--taskPendingCallbackCount == 0) 308 if (--taskPendingCallbackCount == 0)
279 finish(); 309 finish();
280 } 310 }
281 } catch (error) { 311 } catch (error) {
282 var message = 'Uncaught exception:\n' + error.stack; 312 reportError(error);
283 console.error(message);
284 if (!errorReported) {
285 errorReported = true;
286 chrome.metricsPrivate.getIsCrashReportingEnabled(function(isEnabled) {
287 if (isEnabled)
288 sendErrorReport(error);
289 });
290 debugAlert(message);
291 }
292 } 313 }
293 }; 314 };
294 } 315 }
295 316
296 /** 317 /**
297 * Instruments an API function to add error processing to its user 318 * Instruments an API function to add error processing to its user
298 * code-provided callback. 319 * code-provided callback.
299 * @param {Object} namespace Namespace of the API function. 320 * @param {Object} namespace Namespace of the API function.
300 * @param {string} functionName Name of the API function. 321 * @param {string} functionName Name of the API function.
301 * @param {number} callbackParameter Index of the callback parameter to this 322 * @param {number} callbackParameter Index of the callback parameter to this
302 * API function. 323 * API function.
303 */ 324 */
304 function instrumentApiFunction(namespace, functionName, callbackParameter) { 325 function instrumentApiFunction(namespace, functionName, callbackParameter) {
305 var originalFunction = namespace[functionName]; 326 var originalFunction = namespace[functionName];
306 327
307 if (!originalFunction) 328 if (!originalFunction)
308 debugAlert('Cannot instrument ' + functionName); 329 reportError(buildError('Cannot instrument ' + functionName));
309 330
310 namespace[functionName] = function() { 331 namespace[functionName] = function() {
311 // This is the wrapper for the API function. Pass the wrapped callback to 332 // This is the wrapper for the API function. Pass the wrapped callback to
312 // the original function. 333 // the original function.
313 var callback = arguments[callbackParameter]; 334 var callback = arguments[callbackParameter];
314 if (typeof callback != 'function') { 335 if (typeof callback != 'function') {
315 debugAlert('Argument ' + callbackParameter + ' of ' + functionName + 336 reportError(buildError('Argument ' + callbackParameter + ' of ' +
316 ' is not a function'); 337 functionName + ' is not a function'));
317 } 338 }
318 arguments[callbackParameter] = wrapCallback( 339 arguments[callbackParameter] = wrapCallback(
319 callback, functionName == 'addListener'); 340 callback, functionName == 'addListener');
320 return originalFunction.apply(namespace, arguments); 341 return originalFunction.apply(namespace, arguments);
321 }; 342 };
322 } 343 }
323 344
324 instrumentApiFunction(chrome.alarms, 'get', 1); 345 instrumentApiFunction(chrome.alarms, 'get', 1);
325 instrumentApiFunction(chrome.alarms.onAlarm, 'addListener', 0); 346 instrumentApiFunction(chrome.alarms.onAlarm, 'addListener', 0);
326 instrumentApiFunction(chrome.identity, 'getAuthToken', 1); 347 instrumentApiFunction(chrome.identity, 'getAuthToken', 1);
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
528 // Poll for the sign in state every hour. 549 // Poll for the sign in state every hour.
529 // One hour is just an arbitrary amount of time chosen. 550 // One hour is just an arbitrary amount of time chosen.
530 chrome.alarms.create(alarmName, {periodInMinutes: 60}); 551 chrome.alarms.create(alarmName, {periodInMinutes: 60});
531 552
532 return { 553 return {
533 addListener: addListener, 554 addListener: addListener,
534 isSignedIn: isSignedIn, 555 isSignedIn: isSignedIn,
535 removeToken: removeToken 556 removeToken: removeToken
536 }; 557 };
537 } 558 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698