tst.verror.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * tst.verror.js: tests basic functionality of the VError 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.verror.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 VError();
  26. mod_assert.equal(err.name, 'VError');
  27. mod_assert.ok(err instanceof Error);
  28. mod_assert.ok(err instanceof VError);
  29. mod_assert.equal(err.message, '');
  30. mod_assert.ok(err.cause() === undefined);
  31. stack = cleanStack(err.stack);
  32. mod_assert.equal(stack, [
  33. 'VError',
  34. ' at Object.<anonymous> (tst.verror.js)'
  35. ].join('\n') + '\n' + nodestack);
  36. /* options-argument form */
  37. err = new VError({});
  38. mod_assert.equal(err.message, '');
  39. mod_assert.ok(err.cause() === undefined);
  40. /* simple message */
  41. err = new VError('my error');
  42. mod_assert.equal(err.message, 'my error');
  43. mod_assert.ok(err.cause() === undefined);
  44. stack = cleanStack(err.stack);
  45. mod_assert.equal(stack, [
  46. 'VError: my error',
  47. ' at Object.<anonymous> (tst.verror.js)'
  48. ].join('\n') + '\n' + nodestack);
  49. err = new VError({}, 'my error');
  50. mod_assert.equal(err.message, 'my error');
  51. mod_assert.ok(err.cause() === undefined);
  52. /* printf-style message */
  53. err = new VError('%s error: %3d problems', 'very bad', 15);
  54. mod_assert.equal(err.message, 'very bad error: 15 problems');
  55. mod_assert.ok(err.cause() === undefined);
  56. err = new VError({}, '%s error: %3d problems', 'very bad', 15);
  57. mod_assert.equal(err.message, 'very bad error: 15 problems');
  58. mod_assert.ok(err.cause() === undefined);
  59. /* caused by another error, with no additional message */
  60. suberr = new Error('root cause');
  61. err = new VError(suberr);
  62. mod_assert.equal(err.message, ': root cause');
  63. mod_assert.ok(err.cause() === suberr);
  64. err = new VError({ 'cause': suberr });
  65. mod_assert.equal(err.message, ': root cause');
  66. mod_assert.ok(err.cause() === suberr);
  67. /* caused by another error, with annotation */
  68. err = new VError(suberr, 'proximate cause: %d issues', 3);
  69. mod_assert.equal(err.message, 'proximate cause: 3 issues: root cause');
  70. mod_assert.ok(err.cause() === suberr);
  71. stack = cleanStack(err.stack);
  72. mod_assert.equal(stack, [
  73. 'VError: proximate cause: 3 issues: root cause',
  74. ' at Object.<anonymous> (tst.verror.js)'
  75. ].join('\n') + '\n' + nodestack);
  76. err = new VError({ 'cause': suberr }, 'proximate cause: %d issues', 3);
  77. mod_assert.equal(err.message, 'proximate cause: 3 issues: root cause');
  78. mod_assert.ok(err.cause() === suberr);
  79. stack = cleanStack(err.stack);
  80. mod_assert.equal(stack, [
  81. 'VError: proximate cause: 3 issues: root cause',
  82. ' at Object.<anonymous> (tst.verror.js)'
  83. ].join('\n') + '\n' + nodestack);
  84. /* caused by another VError, with annotation. */
  85. suberr = err;
  86. err = new VError(suberr, 'top');
  87. mod_assert.equal(err.message, 'top: proximate cause: 3 issues: root cause');
  88. mod_assert.ok(err.cause() === suberr);
  89. err = new VError({ 'cause': suberr }, 'top');
  90. mod_assert.equal(err.message, 'top: proximate cause: 3 issues: root cause');
  91. mod_assert.ok(err.cause() === suberr);
  92. /* caused by a WError */
  93. suberr = new WError(new Error('root cause'), 'mid');
  94. err = new VError(suberr, 'top');
  95. mod_assert.equal(err.message, 'top: mid');
  96. mod_assert.ok(err.cause() === suberr);
  97. /* null cause (for backwards compatibility with older versions) */
  98. err = new VError(null, 'my error');
  99. mod_assert.equal(err.message, 'my error');
  100. mod_assert.ok(err.cause() === undefined);
  101. stack = cleanStack(err.stack);
  102. mod_assert.equal(stack, [
  103. 'VError: my error',
  104. ' at Object.<anonymous> (tst.verror.js)'
  105. ].join('\n') + '\n' + nodestack);
  106. err = new VError({ 'cause': null }, 'my error');
  107. mod_assert.equal(err.message, 'my error');
  108. mod_assert.ok(err.cause() === undefined);
  109. err = new VError(null);
  110. mod_assert.equal(err.message, '');
  111. mod_assert.ok(err.cause() === undefined);
  112. stack = cleanStack(err.stack);
  113. mod_assert.equal(stack, [
  114. 'VError',
  115. ' at Object.<anonymous> (tst.verror.js)'
  116. ].join('\n') + '\n' + nodestack);
  117. /* constructorOpt */
  118. function makeErr(options) {
  119. return (new VError(options, 'test error'));
  120. }
  121. err = makeErr({});
  122. stack = cleanStack(err.stack);
  123. mod_assert.equal(stack, [
  124. 'VError: test error',
  125. ' at makeErr (tst.verror.js)',
  126. ' at Object.<anonymous> (tst.verror.js)'
  127. ].join('\n') + '\n' + nodestack);
  128. err = makeErr({ 'constructorOpt': makeErr });
  129. stack = cleanStack(err.stack);
  130. mod_assert.equal(stack, [
  131. 'VError: test error',
  132. ' at Object.<anonymous> (tst.verror.js)'
  133. ].join('\n') + '\n' + nodestack);