2020-04-28 15:38:34 -04:00
|
|
|
const { expect } = require('chai')
|
|
|
|
|
2019-07-05 12:55:53 -04:00
|
|
|
const OError = require('..')
|
2020-04-28 15:40:20 -04:00
|
|
|
const {
|
|
|
|
expectError,
|
|
|
|
expectFullStackWithoutStackFramesToEqual,
|
|
|
|
} = require('./support')
|
2019-07-01 06:34:15 -04:00
|
|
|
|
2019-07-05 12:55:53 -04:00
|
|
|
class CustomError1 extends OError {
|
2020-04-28 15:40:20 -04:00
|
|
|
constructor() {
|
|
|
|
super('failed to foo')
|
2019-07-01 06:34:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 12:55:53 -04:00
|
|
|
class CustomError2 extends OError {
|
2020-04-28 15:40:20 -04:00
|
|
|
constructor(customMessage) {
|
|
|
|
super(customMessage || 'failed to bar')
|
2019-07-01 06:34:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 03:44:50 -04:00
|
|
|
describe('OError', function () {
|
2020-04-28 15:40:20 -04:00
|
|
|
it('can have an info object', function () {
|
|
|
|
const err1 = new OError('foo', { foo: 1 })
|
|
|
|
expect(err1.info).to.eql({ foo: 1 })
|
|
|
|
|
|
|
|
const err2 = new OError('foo').withInfo({ foo: 2 })
|
|
|
|
expect(err2.info).to.eql({ foo: 2 })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('can have a cause', function () {
|
|
|
|
const err1 = new OError('foo', { foo: 1 }, new Error('cause 1'))
|
|
|
|
expect(err1.cause.message).to.equal('cause 1')
|
|
|
|
|
|
|
|
const err2 = new OError('foo').withCause(new Error('cause 2'))
|
|
|
|
expect(err2.cause.message).to.equal('cause 2')
|
|
|
|
})
|
|
|
|
|
2020-04-17 03:44:50 -04:00
|
|
|
it('handles a custom error type with a cause', function () {
|
|
|
|
function doSomethingBadInternally() {
|
2019-07-01 06:34:15 -04:00
|
|
|
throw new Error('internal error')
|
|
|
|
}
|
|
|
|
|
2020-04-17 03:44:50 -04:00
|
|
|
function doSomethingBad() {
|
2019-07-01 06:34:15 -04:00
|
|
|
try {
|
|
|
|
doSomethingBadInternally()
|
2020-04-28 15:40:20 -04:00
|
|
|
} catch (error) {
|
|
|
|
throw new CustomError1().withCause(error)
|
2019-07-01 06:34:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
doSomethingBad()
|
|
|
|
expect.fail('should have thrown')
|
2020-04-28 15:40:20 -04:00
|
|
|
} catch (error) {
|
|
|
|
expectError(error, {
|
2019-07-01 06:34:15 -04:00
|
|
|
name: 'CustomError1',
|
|
|
|
klass: CustomError1,
|
2020-04-28 15:40:20 -04:00
|
|
|
message: 'CustomError1: failed to foo',
|
2020-04-17 03:44:50 -04:00
|
|
|
firstFrameRx: /doSomethingBad/,
|
2019-07-01 06:34:15 -04:00
|
|
|
})
|
2020-04-28 15:40:20 -04:00
|
|
|
expect(OError.getFullInfo(error)).to.deep.equal({})
|
|
|
|
expectFullStackWithoutStackFramesToEqual(error, [
|
|
|
|
'CustomError1: failed to foo',
|
|
|
|
'caused by:',
|
|
|
|
' Error: internal error',
|
|
|
|
])
|
2019-07-01 06:34:15 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-04-17 03:44:50 -04:00
|
|
|
it('handles a custom error type with nested causes', function () {
|
|
|
|
function doSomethingBadInternally() {
|
2019-07-01 06:34:15 -04:00
|
|
|
throw new Error('internal error')
|
|
|
|
}
|
|
|
|
|
2020-04-17 03:44:50 -04:00
|
|
|
function doBar() {
|
2019-07-01 06:34:15 -04:00
|
|
|
try {
|
|
|
|
doSomethingBadInternally()
|
2020-04-28 15:40:20 -04:00
|
|
|
} catch (error) {
|
|
|
|
throw new CustomError2('failed to bar!').withCause(error)
|
2019-07-01 06:34:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 03:44:50 -04:00
|
|
|
function doFoo() {
|
2019-07-01 06:34:15 -04:00
|
|
|
try {
|
|
|
|
doBar()
|
2020-04-28 15:40:20 -04:00
|
|
|
} catch (error) {
|
|
|
|
throw new CustomError1().withCause(error)
|
2019-07-01 06:34:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
doFoo()
|
|
|
|
expect.fail('should have thrown')
|
2020-04-28 15:40:20 -04:00
|
|
|
} catch (error) {
|
|
|
|
expectError(error, {
|
2019-07-01 06:34:15 -04:00
|
|
|
name: 'CustomError1',
|
|
|
|
klass: CustomError1,
|
2020-04-28 15:40:20 -04:00
|
|
|
message: 'CustomError1: failed to foo',
|
2020-04-17 03:44:50 -04:00
|
|
|
firstFrameRx: /doFoo/,
|
2019-07-01 06:34:15 -04:00
|
|
|
})
|
2020-04-28 15:40:20 -04:00
|
|
|
expectFullStackWithoutStackFramesToEqual(error, [
|
|
|
|
'CustomError1: failed to foo',
|
|
|
|
'caused by:',
|
|
|
|
' CustomError2: failed to bar!',
|
|
|
|
' caused by:',
|
|
|
|
' Error: internal error',
|
|
|
|
])
|
|
|
|
expect(OError.getFullInfo(error)).to.deep.equal({})
|
2019-07-03 08:10:31 -04:00
|
|
|
}
|
|
|
|
})
|
2019-07-01 06:34:15 -04:00
|
|
|
})
|