Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tst.inherit.js 3.1KB

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