uncaught_exception_handler.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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
  3. // found in the LICENSE file.
  4. // Handles uncaught exceptions thrown by extensions. By default this is to
  5. // log an error message, but tests may override this behaviour.
  6. var handler = function(message, e) {
  7. console.error(message);
  8. };
  9. /**
  10. * Formats the error message and invokes the error handler.
  11. *
  12. * @param {string} message - Error message prefix.
  13. * @param {Error|*} e - Thrown object.
  14. * @param {string=} priorStackTrace - Error message suffix.
  15. * @see formatErrorMessage
  16. */
  17. function handle(message, e, priorStackTrace) {
  18. message = formatErrorMessage(message, e, priorStackTrace);
  19. handler(message, e);
  20. }
  21. // Runs a user-supplied callback safely.
  22. function safeCallbackApply(name, callback, args, priorStackTrace) {
  23. try {
  24. $Function.apply(callback, null, args);
  25. } catch (e) {
  26. handle('Error in response to ' + name, e, priorStackTrace);
  27. }
  28. }
  29. /**
  30. * Append the error description and stack trace to |message|.
  31. *
  32. * @param {string} message - The prefix of the error message.
  33. * @param {Error|*} e - The thrown error object. This object is potentially
  34. * unsafe, because it could be generated by an extension.
  35. * @param {string=} priorStackTrace - The stack trace to be appended to the
  36. * error message. This stack trace must not include stack frames of |e.stack|,
  37. * because both stack traces are concatenated. Overlapping stack traces will
  38. * confuse extension developers.
  39. * @return {string} The formatted error message.
  40. */
  41. function formatErrorMessage(message, e, priorStackTrace) {
  42. if (e)
  43. message += ': ' + safeErrorToString(e, false);
  44. var stack;
  45. try {
  46. // If the stack was set, use it.
  47. // |e.stack| could be void in the following common example:
  48. // throw "Error message";
  49. stack = $String.self(e && e.stack);
  50. } catch (e) {}
  51. // If a stack is not provided, capture a stack trace.
  52. if (!priorStackTrace && !stack)
  53. stack = getStackTrace();
  54. stack = filterExtensionStackTrace(stack);
  55. if (stack)
  56. message += '\n' + stack;
  57. // If an asynchronouse stack trace was set, append it.
  58. if (priorStackTrace)
  59. message += '\n' + priorStackTrace;
  60. return message;
  61. }
  62. function filterExtensionStackTrace(stack) {
  63. if (!stack)
  64. return '';
  65. // Remove stack frames in the stack trace that weren't associated with the
  66. // extension, to not confuse extension developers with internal details.
  67. stack = $String.split(stack, '\n');
  68. stack = $Array.filter(stack, function(line) {
  69. return $String.indexOf(line, 'chrome-extension://') >= 0;
  70. });
  71. return $Array.join(stack, '\n');
  72. }
  73. function getStackTrace() {
  74. var e = {};
  75. $Error.captureStackTrace(e, getStackTrace);
  76. return e.stack;
  77. }
  78. function getExtensionStackTrace() {
  79. return filterExtensionStackTrace(getStackTrace());
  80. }
  81. /**
  82. * Convert an object to a string.
  83. *
  84. * @param {Error|*} e - A thrown object (possibly user-supplied).
  85. * @param {boolean=} omitType - Whether to try to serialize |e.message| instead
  86. * of |e.toString()|.
  87. * @return {string} The error message.
  88. */
  89. function safeErrorToString(e, omitType) {
  90. try {
  91. return $String.self(omitType && e.message || e);
  92. } catch (e) {
  93. // This error is exceptional and could be triggered by
  94. // throw {toString: function() { throw 'Haha' } };
  95. return '(cannot get error message)';
  96. }
  97. }
  98. exports.$set('handle', handle);
  99. // |newHandler| A function which matches |handler|.
  100. exports.$set('setHandler', function(newHandler) {
  101. handler = newHandler;
  102. });
  103. exports.$set('safeCallbackApply', safeCallbackApply);
  104. exports.$set('getStackTrace', getStackTrace);
  105. exports.$set('getExtensionStackTrace', getExtensionStackTrace);
  106. exports.$set('safeErrorToString', safeErrorToString);