overleaf/libraries/o-error/test/o-error.test.js

112 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-04-28 19:38:34 +00:00
const { expect } = require('chai')
2019-07-05 16:55:53 +00:00
const OError = require('..')
2020-04-28 19:40:20 +00:00
const {
expectError,
expectFullStackWithoutStackFramesToEqual,
} = require('./support')
2019-07-05 16:55:53 +00:00
class CustomError1 extends OError {
2020-04-28 19:40:20 +00:00
constructor() {
super('failed to foo')
}
}
2019-07-05 16:55:53 +00:00
class CustomError2 extends OError {
2020-04-28 19:40:20 +00:00
constructor(customMessage) {
super(customMessage || 'failed to bar')
}
}
2020-04-17 07:44:50 +00:00
describe('OError', function () {
2020-04-28 19:40:20 +00: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 07:44:50 +00:00
it('handles a custom error type with a cause', function () {
function doSomethingBadInternally() {
throw new Error('internal error')
}
2020-04-17 07:44:50 +00:00
function doSomethingBad() {
try {
doSomethingBadInternally()
2020-04-28 19:40:20 +00:00
} catch (error) {
throw new CustomError1().withCause(error)
}
}
try {
doSomethingBad()
expect.fail('should have thrown')
2020-04-28 19:40:20 +00:00
} catch (error) {
expectError(error, {
name: 'CustomError1',
klass: CustomError1,
2020-04-28 19:40:20 +00:00
message: 'CustomError1: failed to foo',
2020-04-17 07:44:50 +00:00
firstFrameRx: /doSomethingBad/,
})
2020-04-28 19:40:20 +00:00
expect(OError.getFullInfo(error)).to.deep.equal({})
expectFullStackWithoutStackFramesToEqual(error, [
'CustomError1: failed to foo',
'caused by:',
' Error: internal error',
])
}
})
2020-04-17 07:44:50 +00:00
it('handles a custom error type with nested causes', function () {
function doSomethingBadInternally() {
throw new Error('internal error')
}
2020-04-17 07:44:50 +00:00
function doBar() {
try {
doSomethingBadInternally()
2020-04-28 19:40:20 +00:00
} catch (error) {
throw new CustomError2('failed to bar!').withCause(error)
}
}
2020-04-17 07:44:50 +00:00
function doFoo() {
try {
doBar()
2020-04-28 19:40:20 +00:00
} catch (error) {
throw new CustomError1().withCause(error)
}
}
try {
doFoo()
expect.fail('should have thrown')
2020-04-28 19:40:20 +00:00
} catch (error) {
expectError(error, {
name: 'CustomError1',
klass: CustomError1,
2020-04-28 19:40:20 +00:00
message: 'CustomError1: failed to foo',
2020-04-17 07:44:50 +00:00
firstFrameRx: /doFoo/,
})
2020-04-28 19:40:20 +00: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({})
}
})
})