| 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 var handler = function(message, e) { | 7 var handler = function(message, e) { |
| 8 console.error(message); | 8 console.error(message); |
| 9 }; | 9 }; |
| 10 | 10 |
| 11 /** | 11 /** |
| 12 * Formats the error message and invokes the error handler. |
| 13 * |
| 14 * @param {string} message - Error message prefix. |
| 15 * @param {Error|*} e - Thrown object. |
| 16 * @param {string=} priorStackTrace - Error message suffix. |
| 17 * @see formatErrorMessage |
| 18 */ |
| 19 function handle(message, e, priorStackTrace) { |
| 20 message = formatErrorMessage(message, e, priorStackTrace); |
| 21 handler(message, e); |
| 22 } |
| 23 |
| 24 // Runs a user-supplied callback safely. |
| 25 function safeCallbackApply(name, request, callback, args) { |
| 26 try { |
| 27 $Function.apply(callback, request, args); |
| 28 } catch (e) { |
| 29 handle('Error in response to ' + name, e, request.stack); |
| 30 } |
| 31 } |
| 32 |
| 33 /** |
| 12 * Append the error description and stack trace to |message|. | 34 * Append the error description and stack trace to |message|. |
| 13 * | 35 * |
| 14 * @param {string} message - The prefix of the error message. | 36 * @param {string} message - The prefix of the error message. |
| 15 * @param {Error|*} e - The thrown error object. This object is potentially | 37 * @param {Error|*} e - The thrown error object. This object is potentially |
| 16 * unsafe, because it could be generated by an extension. | 38 * unsafe, because it could be generated by an extension. |
| 17 * @param {string=} priorStackTrace - The stack trace to be appended to the | 39 * @param {string=} priorStackTrace - The stack trace to be appended to the |
| 18 * error message. This stack trace must not include stack frames of |e.stack|, | 40 * error message. This stack trace must not include stack frames of |e.stack|, |
| 19 * because both stack traces are concatenated. Overlapping stack traces will | 41 * because both stack traces are concatenated. Overlapping stack traces will |
| 20 * confuse extension developers. | 42 * confuse extension developers. |
| 21 * @return {string} The formatted error message. | 43 * @return {string} The formatted error message. |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 function safeErrorToString(e, omitType) { | 102 function safeErrorToString(e, omitType) { |
| 81 try { | 103 try { |
| 82 return $String.self(omitType && e.message || e); | 104 return $String.self(omitType && e.message || e); |
| 83 } catch (e) { | 105 } catch (e) { |
| 84 // This error is exceptional and could be triggered by | 106 // This error is exceptional and could be triggered by |
| 85 // throw {toString: function() { throw 'Haha' } }; | 107 // throw {toString: function() { throw 'Haha' } }; |
| 86 return '(cannot get error message)'; | 108 return '(cannot get error message)'; |
| 87 } | 109 } |
| 88 } | 110 } |
| 89 | 111 |
| 90 /** | 112 exports.$set('handle', handle); |
| 91 * Formats the error message and invokes the error handler. | |
| 92 * | |
| 93 * @param {string} message - Error message prefix. | |
| 94 * @param {Error|*} e - Thrown object. | |
| 95 * @param {string=} priorStackTrace - Error message suffix. | |
| 96 * @see formatErrorMessage | |
| 97 */ | |
| 98 exports.$set('handle', function(message, e, priorStackTrace) { | |
| 99 message = formatErrorMessage(message, e, priorStackTrace); | |
| 100 handler(message, e); | |
| 101 }); | |
| 102 | 113 |
| 103 // |newHandler| A function which matches |handler|. | 114 // |newHandler| A function which matches |handler|. |
| 104 exports.$set('setHandler', function(newHandler) { | 115 exports.$set('setHandler', function(newHandler) { |
| 105 handler = newHandler; | 116 handler = newHandler; |
| 106 }); | 117 }); |
| 107 | 118 |
| 119 exports.$set('safeCallbackApply', safeCallbackApply); |
| 108 exports.$set('getStackTrace', getStackTrace); | 120 exports.$set('getStackTrace', getStackTrace); |
| 109 exports.$set('getExtensionStackTrace', getExtensionStackTrace); | 121 exports.$set('getExtensionStackTrace', getExtensionStackTrace); |
| 110 exports.$set('safeErrorToString', safeErrorToString); | 122 exports.$set('safeErrorToString', safeErrorToString); |
| OLD | NEW |