enhanceerror_test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2007 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.debugEnhanceErrorTest');
  15. goog.setTestOnly('goog.debugEnhanceErrorTest');
  16. goog.require('goog.debug');
  17. goog.require('goog.testing.jsunit');
  18. var THROW_STRING = 1;
  19. var THROW_NPE = 2;
  20. var THROW_ERROR = 3;
  21. var THROW_ENHANCED_ERROR = 4;
  22. var THROW_ENHANCED_STRING = 5;
  23. var THROW_OBJECT = 6;
  24. if (typeof debug == 'undefined') {
  25. function debug(str) {
  26. if (window.console) window.console.log(str);
  27. }
  28. }
  29. function testEnhanceError() {
  30. // Tests are like this:
  31. // [test num, expect something in the stack, expect an extra message]
  32. var tests = [
  33. [THROW_STRING], [THROW_OBJECT], [THROW_NPE], [THROW_ERROR],
  34. [THROW_ENHANCED_ERROR, 'throwEnhancedError', 'an enhanced error'],
  35. [THROW_ENHANCED_STRING, 'throwEnhancedString']
  36. ];
  37. for (var i = 0; i < tests.length; ++i) {
  38. var test = tests[i];
  39. var testNum = test[0];
  40. var testInStack = test[1];
  41. var testExtraMessage = test[2] || null;
  42. try {
  43. foo(testNum);
  44. } catch (e) {
  45. debug(goog.debug.expose(e));
  46. var s = e.stack.split('\n');
  47. for (var j = 0; j < s.length; ++j) {
  48. debug(s[j]);
  49. }
  50. // 'baz' is always in the stack
  51. assertTrue('stack should contain "baz"', e.stack.indexOf('baz') != -1);
  52. if (testInStack) {
  53. assertTrue(
  54. 'stack should contain "' + testInStack + '"',
  55. e.stack.indexOf(testInStack) != -1);
  56. }
  57. if (testExtraMessage) {
  58. // 2 messages
  59. assertTrue(
  60. 'message0 should contain "' + testExtraMessage + '"',
  61. e.message0.indexOf(testExtraMessage) != -1);
  62. assertTrue(
  63. 'message1 should contain "message from baz"',
  64. e.message1.indexOf('message from baz') != -1);
  65. } else {
  66. // 1 message
  67. assertTrue(
  68. 'message0 should contain "message from baz"',
  69. e.message0.indexOf('message from baz') != -1);
  70. }
  71. continue;
  72. }
  73. fail('expected to catch an exception');
  74. }
  75. }
  76. function foo(testNum) {
  77. bar(testNum);
  78. }
  79. function bar(testNum) {
  80. baz(testNum);
  81. }
  82. function baz(testNum) {
  83. try {
  84. switch (testNum) {
  85. case THROW_STRING:
  86. throwString();
  87. break;
  88. case THROW_NPE:
  89. throwNpe();
  90. break;
  91. case THROW_ERROR:
  92. throwError();
  93. break;
  94. case THROW_ENHANCED_ERROR:
  95. throwEnhancedError();
  96. break;
  97. case THROW_ENHANCED_STRING:
  98. throwEnhancedString();
  99. break;
  100. case THROW_OBJECT:
  101. throwObject();
  102. break;
  103. }
  104. } catch (e) {
  105. throw goog.debug.enhanceError(e, 'message from baz');
  106. }
  107. }
  108. function throwString() {
  109. throw 'a string';
  110. }
  111. function throwNpe() {
  112. var nada = null;
  113. nada.noSuchFunction();
  114. }
  115. function throwError() {
  116. throw Error('an error');
  117. }
  118. function throwEnhancedError() {
  119. throw goog.debug.enhanceError(Error('dang!'), 'an enhanced error');
  120. }
  121. function throwEnhancedString() {
  122. throw goog.debug.enhanceError('oh nos!');
  123. }
  124. /**
  125. * @throws {*}
  126. */
  127. function throwObject() {
  128. throw {property: 'value'};
  129. }