Chromium Code Reviews| 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 // Handles uncaught exceptions thrown by extensions. By default this is to | 5 // Handles uncaught exceptions thrown by extensions. By default this is to |
| 6 // log an error message, but tests may override this behaviour. | 6 // log an error message, but tests may override this behaviour. |
| 7 | |
| 8 var handler = function(message, e) { | 7 var handler = function(message, e) { |
| 9 console.error(message); | 8 console.error(message); |
| 10 }; | 9 }; |
| 11 | 10 |
| 12 // |message| The message associated with the error. | 11 /** |
| 13 // |e| The object that was thrown. | 12 * Append the error description and stack trace to |message|. |
| 14 exports.handle = function(message, e) { | 13 * |
| 14 * @param {string} message - The prefix of the error message. | |
| 15 * @param {Error|*} e - The thrown error object. This object is potentially | |
| 16 * unsafe, because it could be generated by an extension. | |
| 17 * @param {string=} stackTrace - The stack trace to be appended to the error | |
| 18 * message. This stack trace must not include stack frames of |e.stack|. | |
|
not at google - send to devlin
2014/08/18 23:13:11
Why can't it include stack frames of |e.stack|?
robwu
2014/08/18 23:27:18
The stack traces are concatenated. If the stack tr
not at google - send to devlin
2014/08/18 23:39:16
Ah I understand. Perhaps "priorStackTrace" or simi
robwu
2014/08/19 14:21:54
Done.
| |
| 19 * @return {string} The formatted error message. | |
| 20 */ | |
| 21 function formatErrorMessage(message, e, stackTrace) { | |
| 22 // Append ": [error message]" | |
| 23 if (e) { | |
| 24 try { | |
| 25 // Append the toString() representation of |e| to the message. For | |
| 26 // instances of Error, it looks like "Error: <message>". | |
| 27 message += ': ' + e; | |
| 28 } catch (e) { | |
| 29 // This could be triggered by | |
| 30 // throw {toString: function() { throw 'Haha' } }; | |
| 31 message += ': (cannot get error message)'; | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 var stack; | |
| 36 try { | |
| 37 // If the stack was set, use it. | |
| 38 // |e.stack| could be void in the following common example: | |
| 39 // throw "Error message"; | |
| 40 stack = $String.self(e && e.stack); | |
| 41 } catch (e) {} | |
| 42 | |
| 43 // If a stack is not provided, capture a stack trace. | |
| 44 if (!stackTrace && !stack) | |
| 45 stack = getStackTrace(); | |
| 46 | |
| 47 stack = filterExtensionStackTrace(stack); | |
| 48 if (stack) | |
| 49 message += '\n' + stack; | |
| 50 | |
| 51 // If an asynchronouse stack trace was set, append it. | |
| 52 if (stackTrace) | |
| 53 message += '\n' + stackTrace; | |
| 54 | |
| 55 return message; | |
| 56 } | |
| 57 | |
| 58 function filterExtensionStackTrace(stack) { | |
| 59 if (!stack) | |
| 60 return ''; | |
| 61 // Remove stack frames in the stack trace that weren't associated with the | |
| 62 // extension, to not confuse extension developers with internal details. | |
| 63 stack = $String.split(stack, '\n'); | |
| 64 stack = $Array.filter(stack, function(line) { | |
| 65 return line.indexOf('chrome-extension://') >= 0; | |
| 66 }); | |
| 67 return $Array.join(stack, '\n'); | |
| 68 } | |
| 69 | |
| 70 // Get a local reference to the captureStackTrace method to prevent extension | |
| 71 // code from interfering with our method. | |
| 72 var captureStackTrace = Error.captureStackTrace; | |
| 73 function getStackTrace() { | |
| 74 var e = {}; | |
| 75 captureStackTrace(e, getStackTrace); | |
| 76 return e.stack; | |
| 77 } | |
| 78 | |
| 79 function getExtensionStackTrace() { | |
| 80 return filterExtensionStackTrace(getStackTrace()); | |
| 81 } | |
| 82 | |
| 83 /** | |
| 84 * Formats the error message and invokes the error handler. | |
| 85 * | |
| 86 * @param {string} message - Error message prefix. | |
| 87 * @param {Error|*} e - Thrown object. | |
| 88 * @param {string=} stackTrace - Error message suffix. | |
| 89 * @see formatErrorMessage | |
| 90 */ | |
| 91 exports.handle = function(message, e, stackTrace) { | |
| 92 message = formatErrorMessage(message, e, stackTrace); | |
| 15 handler(message, e); | 93 handler(message, e); |
| 16 }; | 94 }; |
| 17 | 95 |
| 18 // |newHandler| A function which matches |exports.handle|. | 96 // |newHandler| A function which matches |handler|. |
| 19 exports.setHandler = function(newHandler) { | 97 exports.setHandler = function(newHandler) { |
| 20 handler = newHandler; | 98 handler = newHandler; |
| 21 }; | 99 }; |
| 100 | |
| 101 exports.getStackTrace = getStackTrace; | |
| 102 exports.getExtensionStackTrace = getExtensionStackTrace; | |
| OLD | NEW |