tst.inherit.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * tst.inherit.js: test that inheriting from VError and WError work as expected.
  3. */
  4. var mod_assert = require('assert');
  5. var mod_util = require('util');
  6. var mod_verror = require('../lib/verror');
  7. var VError = mod_verror.VError;
  8. var WError = mod_verror.WError;
  9. var err, suberr;
  10. function VErrorChild()
  11. {
  12. VError.apply(this, Array.prototype.slice.call(arguments));
  13. }
  14. mod_util.inherits(VErrorChild, VError);
  15. VErrorChild.prototype.name = 'VErrorChild';
  16. function WErrorChild()
  17. {
  18. WError.apply(this, Array.prototype.slice.call(arguments));
  19. }
  20. mod_util.inherits(WErrorChild, WError);
  21. WErrorChild.prototype.name = 'WErrorChild';
  22. suberr = new Error('root cause');
  23. err = new VErrorChild(suberr, 'top');
  24. mod_assert.ok(err instanceof Error);
  25. mod_assert.ok(err instanceof VError);
  26. mod_assert.ok(err instanceof VErrorChild);
  27. mod_assert.equal(err.cause(), suberr);
  28. mod_assert.equal(err.message, 'top: root cause');
  29. mod_assert.equal(err.toString(), 'VErrorChild: top: root cause');
  30. mod_assert.equal(err.stack.split('\n')[0], 'VErrorChild: top: root cause');
  31. suberr = new Error('root cause');
  32. err = new WErrorChild(suberr, 'top');
  33. mod_assert.ok(err instanceof Error);
  34. mod_assert.ok(err instanceof WError);
  35. mod_assert.ok(err instanceof WErrorChild);
  36. mod_assert.equal(err.cause(), suberr);
  37. mod_assert.equal(err.message, 'top');
  38. mod_assert.equal(err.toString(),
  39. 'WErrorChild: top; caused by Error: root cause');
  40. mod_assert.equal(err.stack.split('\n')[0],
  41. 'WErrorChild: top; caused by Error: root cause');
  42. // Test that `<Ctor>.toString()` uses the ctor name. I.e. setting
  43. // `<Ctor>.prototype.name` isn't necessary.
  44. function VErrorChildNoName() {
  45. VError.apply(this, Array.prototype.slice.call(arguments));
  46. }
  47. mod_util.inherits(VErrorChildNoName, VError);
  48. err = new VErrorChildNoName('top');
  49. mod_assert.equal(err.toString(), 'VErrorChildNoName: top');
  50. function WErrorChildNoName() {
  51. WError.apply(this, Array.prototype.slice.call(arguments));
  52. }
  53. mod_util.inherits(WErrorChildNoName, WError);
  54. err = new WErrorChildNoName('top');
  55. mod_assert.equal(err.toString(), 'WErrorChildNoName: top');
  56. // Test that `<Ctor>.prototype.name` can be used for the `.toString()`
  57. // when the ctor is anonymous.
  58. var VErrorChildAnon = function () {
  59. VError.apply(this, Array.prototype.slice.call(arguments));
  60. };
  61. mod_util.inherits(VErrorChildAnon, VError);
  62. VErrorChildAnon.prototype.name = 'VErrorChildAnon';
  63. err = new VErrorChildAnon('top');
  64. mod_assert.equal(err.toString(), 'VErrorChildAnon: top');
  65. var WErrorChildAnon = function () {
  66. WError.apply(this, Array.prototype.slice.call(arguments));
  67. };
  68. mod_util.inherits(WErrorChildAnon, WError);
  69. WErrorChildAnon.prototype.name = 'WErrorChildAnon';
  70. err = new WErrorChildAnon('top');
  71. mod_assert.equal(err.toString(), 'WErrorChildAnon: top');
  72. // Test get appropriate exception name in `.toString()` when reconstituting
  73. // an error instance a la:
  74. // https://github.com/mcavage/node-fast/blob/master/lib/client.js#L215
  75. err = new VError('top');
  76. err.name = 'CustomNameError';
  77. mod_assert.equal(err.toString(), 'CustomNameError: top');
  78. err = new WError('top');
  79. err.name = 'CustomNameError';
  80. mod_assert.equal(err.toString(), 'CustomNameError: top');