tst.werror.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * tst.werror.js: tests basic functionality of the WError class.
  3. */
  4. var mod_assert = require('assert');
  5. var mod_verror = require('../lib/verror');
  6. var VError = mod_verror.VError;
  7. var WError = mod_verror.WError;
  8. var err, suberr, stack, substack;
  9. /*
  10. * Remove full paths and relative line numbers from stack traces so that we can
  11. * compare against "known-good" output.
  12. */
  13. function cleanStack(stacktxt)
  14. {
  15. var re = new RegExp(__filename + ':\\d+:\\d+', 'gm');
  16. stacktxt = stacktxt.replace(re, 'tst.werror.js');
  17. return (stacktxt);
  18. }
  19. /*
  20. * Save the generic parts of all stack traces so we can avoid hardcoding
  21. * Node-specific implementation details in our testing of stack traces.
  22. */
  23. var nodestack = new Error().stack.split('\n').slice(2).join('\n');
  24. /* no arguments */
  25. err = new WError();
  26. mod_assert.equal(err.name, 'WError');
  27. mod_assert.ok(err instanceof Error);
  28. mod_assert.ok(err instanceof WError);
  29. mod_assert.equal(err.message, '');
  30. mod_assert.equal(err.toString(), 'WError');
  31. mod_assert.ok(err.cause() === undefined);
  32. stack = cleanStack(err.stack);
  33. mod_assert.equal(stack, [
  34. 'WError',
  35. ' at Object.<anonymous> (tst.werror.js)'
  36. ].join('\n') + '\n' + nodestack);
  37. /* options-argument form */
  38. err = new WError({});
  39. mod_assert.equal(err.message, '');
  40. mod_assert.equal(err.toString(), 'WError');
  41. mod_assert.ok(err.cause() === undefined);
  42. /* simple message */
  43. err = new WError('my error');
  44. mod_assert.equal(err.message, 'my error');
  45. mod_assert.equal(err.toString(), 'WError: my error');
  46. mod_assert.ok(err.cause() === undefined);
  47. stack = cleanStack(err.stack);
  48. mod_assert.equal(stack, [
  49. 'WError: my error',
  50. ' at Object.<anonymous> (tst.werror.js)'
  51. ].join('\n') + '\n' + nodestack);
  52. err = new WError({}, 'my error');
  53. mod_assert.equal(err.message, 'my error');
  54. mod_assert.equal(err.toString(), 'WError: my error');
  55. mod_assert.ok(err.cause() === undefined);
  56. /* printf-style message */
  57. err = new WError('%s error: %3d problems', 'very bad', 15);
  58. mod_assert.equal(err.message, 'very bad error: 15 problems');
  59. mod_assert.equal(err.toString(), 'WError: very bad error: 15 problems');
  60. mod_assert.ok(err.cause() === undefined);
  61. err = new WError({}, '%s error: %3d problems', 'very bad', 15);
  62. mod_assert.equal(err.message, 'very bad error: 15 problems');
  63. mod_assert.equal(err.toString(), 'WError: very bad error: 15 problems');
  64. mod_assert.ok(err.cause() === undefined);
  65. /* caused by another error, with no additional message */
  66. suberr = new Error('root cause');
  67. err = new WError(suberr);
  68. mod_assert.equal(err.message, '');
  69. mod_assert.equal(err.toString(), 'WError; caused by Error: root cause');
  70. mod_assert.ok(err.cause() === suberr);
  71. err = new WError({ 'cause': suberr });
  72. mod_assert.equal(err.message, '');
  73. mod_assert.equal(err.toString(), 'WError; caused by Error: root cause');
  74. mod_assert.ok(err.cause() === suberr);
  75. /* caused by another error, with annotation */
  76. err = new WError(suberr, 'proximate cause: %d issues', 3);
  77. mod_assert.equal(err.message, 'proximate cause: 3 issues');
  78. mod_assert.equal(err.toString(), 'WError: proximate cause: 3 issues; ' +
  79. 'caused by Error: root cause');
  80. mod_assert.ok(err.cause() === suberr);
  81. stack = cleanStack(err.stack);
  82. mod_assert.equal(stack, [
  83. 'WError: proximate cause: 3 issues; caused by Error: root cause',
  84. ' at Object.<anonymous> (tst.werror.js)'
  85. ].join('\n') + '\n' + nodestack);
  86. err = new WError({ 'cause': suberr }, 'proximate cause: %d issues', 3);
  87. mod_assert.equal(err.message, 'proximate cause: 3 issues');
  88. mod_assert.equal(err.toString(), 'WError: proximate cause: 3 issues; ' +
  89. 'caused by Error: root cause');
  90. mod_assert.ok(err.cause() === suberr);
  91. stack = cleanStack(err.stack);
  92. mod_assert.equal(stack, [
  93. 'WError: proximate cause: 3 issues; caused by Error: root cause',
  94. ' at Object.<anonymous> (tst.werror.js)'
  95. ].join('\n') + '\n' + nodestack);
  96. /* caused by another WError, with annotation. */
  97. suberr = err;
  98. err = new WError(suberr, 'top');
  99. mod_assert.equal(err.message, 'top');
  100. mod_assert.equal(err.toString(), 'WError: top; caused by WError: ' +
  101. 'proximate cause: 3 issues; caused by Error: root cause');
  102. mod_assert.ok(err.cause() === suberr);
  103. err = new WError({ 'cause': suberr }, 'top');
  104. mod_assert.equal(err.message, 'top');
  105. mod_assert.equal(err.toString(), 'WError: top; caused by WError: ' +
  106. 'proximate cause: 3 issues; caused by Error: root cause');
  107. mod_assert.ok(err.cause() === suberr);
  108. /* caused by a VError */
  109. suberr = new VError(new Error('root cause'), 'mid');
  110. err = new WError(suberr, 'top');
  111. mod_assert.equal(err.message, 'top');
  112. mod_assert.equal(err.toString(),
  113. 'WError: top; caused by VError: mid: root cause');
  114. mod_assert.ok(err.cause() === suberr);
  115. /* null cause (for backwards compatibility with older versions) */
  116. err = new WError(null, 'my error');
  117. mod_assert.equal(err.message, 'my error');
  118. mod_assert.equal(err.toString(), 'WError: my error');
  119. mod_assert.ok(err.cause() === undefined);
  120. stack = cleanStack(err.stack);
  121. mod_assert.equal(stack, [
  122. 'WError: my error',
  123. ' at Object.<anonymous> (tst.werror.js)'
  124. ].join('\n') + '\n' + nodestack);
  125. err = new WError({ 'cause': null }, 'my error');
  126. mod_assert.equal(err.message, 'my error');
  127. mod_assert.equal(err.toString(), 'WError: my error');
  128. mod_assert.ok(err.cause() === undefined);
  129. err = new WError(null);
  130. mod_assert.equal(err.message, '');
  131. mod_assert.equal(err.toString(), 'WError');
  132. mod_assert.ok(err.cause() === undefined);
  133. stack = cleanStack(err.stack);
  134. mod_assert.equal(stack, [
  135. 'WError',
  136. ' at Object.<anonymous> (tst.werror.js)'
  137. ].join('\n') + '\n' + nodestack);
  138. /* constructorOpt */
  139. function makeErr(options) {
  140. return (new WError(options, 'test error'));
  141. }
  142. err = makeErr({});
  143. mod_assert.equal(err.toString(), 'WError: test error');
  144. stack = cleanStack(err.stack);
  145. mod_assert.equal(stack, [
  146. 'WError: test error',
  147. ' at makeErr (tst.werror.js)',
  148. ' at Object.<anonymous> (tst.werror.js)'
  149. ].join('\n') + '\n' + nodestack);
  150. err = makeErr({ 'constructorOpt': makeErr });
  151. mod_assert.equal(err.toString(), 'WError: test error');
  152. stack = cleanStack(err.stack);
  153. mod_assert.equal(stack, [
  154. 'WError: test error',
  155. ' at Object.<anonymous> (tst.werror.js)'
  156. ].join('\n') + '\n' + nodestack);