o-error.test.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const { expect } = require('chai')
  2. const OError = require('..')
  3. const {
  4. expectError,
  5. expectFullStackWithoutStackFramesToEqual,
  6. } = require('./support')
  7. class CustomError1 extends OError {
  8. constructor() {
  9. super('failed to foo')
  10. }
  11. }
  12. class CustomError2 extends OError {
  13. constructor(customMessage) {
  14. super(customMessage || 'failed to bar')
  15. }
  16. }
  17. describe('OError', function () {
  18. it('can have an info object', function () {
  19. const err1 = new OError('foo', { foo: 1 })
  20. expect(err1.info).to.eql({ foo: 1 })
  21. const err2 = new OError('foo').withInfo({ foo: 2 })
  22. expect(err2.info).to.eql({ foo: 2 })
  23. })
  24. it('can have a cause', function () {
  25. const err1 = new OError('foo', { foo: 1 }, new Error('cause 1'))
  26. expect(err1.cause.message).to.equal('cause 1')
  27. const err2 = new OError('foo').withCause(new Error('cause 2'))
  28. expect(err2.cause.message).to.equal('cause 2')
  29. })
  30. it('handles a custom error type with a cause', function () {
  31. function doSomethingBadInternally() {
  32. throw new Error('internal error')
  33. }
  34. function doSomethingBad() {
  35. try {
  36. doSomethingBadInternally()
  37. } catch (error) {
  38. throw new CustomError1().withCause(error)
  39. }
  40. }
  41. try {
  42. doSomethingBad()
  43. expect.fail('should have thrown')
  44. } catch (error) {
  45. expectError(error, {
  46. name: 'CustomError1',
  47. klass: CustomError1,
  48. message: 'CustomError1: failed to foo',
  49. firstFrameRx: /doSomethingBad/,
  50. })
  51. expect(OError.getFullInfo(error)).to.deep.equal({})
  52. expectFullStackWithoutStackFramesToEqual(error, [
  53. 'CustomError1: failed to foo',
  54. 'caused by:',
  55. ' Error: internal error',
  56. ])
  57. }
  58. })
  59. it('handles a custom error type with nested causes', function () {
  60. function doSomethingBadInternally() {
  61. throw new Error('internal error')
  62. }
  63. function doBar() {
  64. try {
  65. doSomethingBadInternally()
  66. } catch (error) {
  67. throw new CustomError2('failed to bar!').withCause(error)
  68. }
  69. }
  70. function doFoo() {
  71. try {
  72. doBar()
  73. } catch (error) {
  74. throw new CustomError1().withCause(error)
  75. }
  76. }
  77. try {
  78. doFoo()
  79. expect.fail('should have thrown')
  80. } catch (error) {
  81. expectError(error, {
  82. name: 'CustomError1',
  83. klass: CustomError1,
  84. message: 'CustomError1: failed to foo',
  85. firstFrameRx: /doFoo/,
  86. })
  87. expectFullStackWithoutStackFramesToEqual(error, [
  88. 'CustomError1: failed to foo',
  89. 'caused by:',
  90. ' CustomError2: failed to bar!',
  91. ' caused by:',
  92. ' Error: internal error',
  93. ])
  94. expect(OError.getFullInfo(error)).to.deep.equal({})
  95. }
  96. })
  97. })