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 // 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
| |
13 // |e| The object that was thrown. | 12 // "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
| |
14 exports.handle = function(message, e) { | 13 function formatErrorMessage(message, e, asyncStack) { |
14 // Append ": [error message]" | |
15 if (e) { | |
16 try { | |
17 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.
| |
18 } catch (e) { | |
19 // This could be triggered by | |
20 // throw {toString: function() { throw 'Haha' } }; | |
21 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
| |
22 } | |
23 } | |
24 | |
25 var stack; | |
26 try { | |
27 // If the stack was set, use it. | |
28 // e.stack could be void in the following common example: | |
29 // throw "Error message"; | |
30 stack = $String.self(e && e.stack); | |
31 } 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
| |
32 | |
33 // If a stack is not provided, capture a stack trace. | |
34 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
| |
35 stack = getStackTrace(); | |
36 | |
37 stack = filterExtensionStackTrace(stack); | |
38 if (stack) | |
39 message += '\n' + stack; | |
40 | |
41 // If an asynchronouse stack trace was set, append it. | |
42 if (asyncStack) | |
43 message += '\n' + asyncStack; | |
44 | |
45 return message; | |
46 } | |
47 | |
48 function filterExtensionStackTrace(stack) { | |
49 if (!stack) | |
50 return ''; | |
51 // 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.
| |
52 // extension, to not confuse extension developers with internal details. | |
53 stack = $String.split(stack, '\n'); | |
54 stack = $Array.filter(stack, function(line) { | |
55 return line.indexOf('chrome-extension://') >= 0; | |
56 }); | |
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
| |
57 return $Array.join(stack, '\n'); | |
58 } | |
59 | |
60 // Get a local reference to the captureStackTrace method to prevent extension | |
61 // code from interfering with our method. | |
62 var captureStackTrace = Error.captureStackTrace; | |
63 function getStackTrace() { | |
64 var e = {}; | |
65 captureStackTrace(e, getStackTrace); | |
not at google - send to devlin
2014/08/18 16:09:45
Neat! I didn't know about this method.
| |
66 return e.stack; | |
67 } | |
68 | |
69 function getExtensionStackTrace() { | |
70 return filterExtensionStackTrace(getStackTrace()); | |
71 } | |
72 | |
73 // |message| The message associated with the error. | |
74 // |e| The object that was thrown. | |
75 // |asyncStack| An optional stack trace to be appended to the message. | |
76 // It should not already be a part of e.stack. | |
77 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.
| |
78 message = formatErrorMessage(message, e, asyncStack); | |
15 handler(message, e); | 79 handler(message, e); |
16 }; | 80 }; |
17 | 81 |
18 // |newHandler| A function which matches |exports.handle|. | 82 // |newHandler| A function which matches |handler|. |
19 exports.setHandler = function(newHandler) { | 83 exports.setHandler = function(newHandler) { |
20 handler = newHandler; | 84 handler = newHandler; |
21 }; | 85 }; |
86 | |
87 exports.getStackTrace = getStackTrace; | |
88 exports.getExtensionStackTrace = getExtensionStackTrace; | |
OLD | NEW |