Chromium Code Reviews| Index: extensions/renderer/resources/uncaught_exception_handler.js |
| diff --git a/extensions/renderer/resources/uncaught_exception_handler.js b/extensions/renderer/resources/uncaught_exception_handler.js |
| index a3709b58f3c0f6c0e9de5d31e88e2381c5e167c5..58e81b114a9178ace41f3bf4cae100e09c559e67 100644 |
| --- a/extensions/renderer/resources/uncaught_exception_handler.js |
| +++ b/extensions/renderer/resources/uncaught_exception_handler.js |
| @@ -4,18 +4,85 @@ |
| // Handles uncaught exceptions thrown by extensions. By default this is to |
| // log an error message, but tests may override this behaviour. |
| - |
| var handler = function(message, e) { |
| console.error(message); |
| }; |
| -// |message| The message associated with the error. |
| -// |e| The object that was thrown. |
| -exports.handle = function(message, e) { |
| +// message and asyncStack are safe strings, |
|
not at google - send to devlin
2014/08/18 16:09:45
Let's update to this to use JSDoc-style annotation
robwu
2014/08/18 20:59:47
Done. http://usejsdoc.org is also a decent referen
|
| +// "e" could be a user-supplied object. |
|
Finnur
2014/08/18 11:50:14
It is a bit weird to see "e", given that the usual
robwu
2014/08/18 15:37:32
Done (will send updated patch set after the review
not at google - send to devlin
2014/08/18 16:09:45
I also preferred the old message; "could be user-s
|
| +function formatErrorMessage(message, e, asyncStack) { |
| + // Append ": [error message]" |
| + if (e) { |
| + try { |
| + message += ': ' + e; |
|
not at google - send to devlin
2014/08/18 16:09:45
You're relying on toString being defined for the e
robwu
2014/08/18 20:59:46
Done.
|
| + } catch (e) { |
| + // This could be triggered by |
| + // throw {toString: function() { throw 'Haha' } }; |
| + message += ': (cannot get error message)'; |
|
not at google - send to devlin
2014/08/18 16:09:45
"Unknown error"?
robwu
2014/08/18 20:59:47
I've chosen "Cannot get error message" because it
|
| + } |
| + } |
| + |
| + var stack; |
| + try { |
| + // If the stack was set, use it. |
| + // e.stack could be void in the following common example: |
| + // throw "Error message"; |
| + stack = $String.self(e && e.stack); |
| + } catch (e) {} |
|
not at google - send to devlin
2014/08/18 16:09:45
When can this throw?
robwu
2014/08/18 20:59:47
When an object with a throwing "get" getter is thr
|
| + |
| + // If a stack is not provided, capture a stack trace. |
| + if (!asyncStack && !stack) |
|
not at google - send to devlin
2014/08/18 16:09:45
"asyncStack" assumes the intent of the caller was
robwu
2014/08/18 20:59:47
Done.
(for your specific example, note that if tr
|
| + stack = getStackTrace(); |
| + |
| + stack = filterExtensionStackTrace(stack); |
| + if (stack) |
| + message += '\n' + stack; |
| + |
| + // If an asynchronouse stack trace was set, append it. |
| + if (asyncStack) |
| + message += '\n' + asyncStack; |
| + |
| + return message; |
| +} |
| + |
| +function filterExtensionStackTrace(stack) { |
| + if (!stack) |
| + return ''; |
| + // Remove stack frames in the stack trace that and weren't associated with the |
|
Finnur
2014/08/18 11:50:14
Remove the word 'that'?
robwu
2014/08/18 15:37:32
Removed "and", thanks for spotting.
|
| + // extension, to not confuse extension developers with internal details. |
| + stack = $String.split(stack, '\n'); |
| + stack = $Array.filter(stack, function(line) { |
| + return line.indexOf('chrome-extension://') >= 0; |
| + }); |
|
Finnur
2014/08/18 11:50:14
Is this filtering a necessary part of the solution
robwu
2014/08/18 15:37:32
The original stack handling logic also filtered th
|
| + return $Array.join(stack, '\n'); |
| +} |
| + |
| +// Get a local reference to the captureStackTrace method to prevent extension |
| +// code from interfering with our method. |
| +var captureStackTrace = Error.captureStackTrace; |
| +function getStackTrace() { |
| + var e = {}; |
| + captureStackTrace(e, getStackTrace); |
|
not at google - send to devlin
2014/08/18 16:09:45
Neat! I didn't know about this method.
|
| + return e.stack; |
| +} |
| + |
| +function getExtensionStackTrace() { |
| + return filterExtensionStackTrace(getStackTrace()); |
| +} |
| + |
| +// |message| The message associated with the error. |
| +// |e| The object that was thrown. |
| +// |asyncStack| An optional stack trace to be appended to the message. |
| +// It should not already be a part of e.stack. |
| +exports.handle = function(message, e, asyncStack) { |
|
not at google - send to devlin
2014/08/18 16:09:45
Also JSDoc this method (basically: any public meth
robwu
2014/08/18 20:59:47
Done.
|
| + message = formatErrorMessage(message, e, asyncStack); |
| handler(message, e); |
| }; |
| -// |newHandler| A function which matches |exports.handle|. |
| +// |newHandler| A function which matches |handler|. |
| exports.setHandler = function(newHandler) { |
| handler = newHandler; |
| }; |
| + |
| +exports.getStackTrace = getStackTrace; |
| +exports.getExtensionStackTrace = getExtensionStackTrace; |