2020-04-28 15:38:34 -04:00
|
|
|
const { expect } = require('chai')
|
2020-04-28 15:40:20 -04:00
|
|
|
const { promisify } = require('util')
|
|
|
|
|
|
|
|
const OError = require('..')
|
|
|
|
|
|
|
|
const {
|
|
|
|
expectError,
|
|
|
|
expectFullStackWithoutStackFramesToEqual,
|
|
|
|
} = require('./support')
|
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
describe('utils', function () {
|
|
|
|
describe('OError.tag', function () {
|
|
|
|
it('tags errors thrown from an async function', async function () {
|
|
|
|
const delay = promisify(setTimeout)
|
|
|
|
|
|
|
|
async function foo() {
|
|
|
|
await delay(10)
|
|
|
|
throw new Error('foo error')
|
2020-04-28 15:40:20 -04:00
|
|
|
}
|
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
async function bar() {
|
|
|
|
try {
|
|
|
|
await foo()
|
|
|
|
} catch (error) {
|
|
|
|
throw OError.tag(error, 'failed to bar', { bar: 'baz' })
|
|
|
|
}
|
2020-04-28 15:40:20 -04:00
|
|
|
}
|
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
async function baz() {
|
|
|
|
try {
|
|
|
|
await bar()
|
|
|
|
} catch (error) {
|
|
|
|
throw OError.tag(error, 'failed to baz', { baz: 'bat' })
|
|
|
|
}
|
2020-04-28 15:40:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-12-16 04:04:32 -05:00
|
|
|
await baz()
|
|
|
|
expect.fail('should have thrown')
|
2020-04-28 15:40:20 -04:00
|
|
|
} catch (error) {
|
2021-12-16 04:04:32 -05:00
|
|
|
expectError(error, {
|
|
|
|
name: 'Error',
|
|
|
|
klass: Error,
|
|
|
|
message: 'Error: foo error',
|
|
|
|
firstFrameRx: /at foo/,
|
|
|
|
})
|
|
|
|
expectFullStackWithoutStackFramesToEqual(error, [
|
|
|
|
'Error: foo error',
|
|
|
|
'TaggedError: failed to bar',
|
|
|
|
'TaggedError: failed to baz',
|
|
|
|
])
|
|
|
|
expect(OError.getFullInfo(error)).to.eql({
|
|
|
|
bar: 'baz',
|
|
|
|
baz: 'bat',
|
|
|
|
})
|
2020-04-28 15:40:20 -04:00
|
|
|
}
|
2021-12-16 04:04:32 -05:00
|
|
|
})
|
2020-04-28 15:40:20 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
it('tags errors thrown from a promise rejection', async function () {
|
|
|
|
function foo() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
reject(new Error('foo error'))
|
|
|
|
}, 10)
|
|
|
|
})
|
|
|
|
}
|
2020-04-28 15:40:20 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
async function bar() {
|
|
|
|
try {
|
|
|
|
await foo()
|
|
|
|
} catch (error) {
|
|
|
|
throw OError.tag(error, 'failed to bar', { bar: 'baz' })
|
2020-04-28 15:40:20 -04:00
|
|
|
}
|
2021-12-16 04:04:32 -05:00
|
|
|
}
|
2020-04-28 15:40:20 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
async function baz() {
|
|
|
|
try {
|
|
|
|
await bar()
|
|
|
|
} catch (error) {
|
|
|
|
throw OError.tag(error, 'failed to baz', { baz: 'bat' })
|
2020-04-28 15:40:20 -04:00
|
|
|
}
|
2021-12-16 04:04:32 -05:00
|
|
|
}
|
2020-04-28 15:40:20 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
try {
|
|
|
|
await baz()
|
|
|
|
expect.fail('should have thrown')
|
|
|
|
} catch (error) {
|
|
|
|
expectError(error, {
|
2020-04-28 15:40:20 -04:00
|
|
|
name: 'Error',
|
|
|
|
klass: Error,
|
|
|
|
message: 'Error: foo error',
|
|
|
|
firstFrameRx: /_onTimeout/,
|
|
|
|
})
|
2021-12-16 04:04:32 -05:00
|
|
|
expectFullStackWithoutStackFramesToEqual(error, [
|
2020-04-28 15:40:20 -04:00
|
|
|
'Error: foo error',
|
|
|
|
'TaggedError: failed to bar',
|
|
|
|
'TaggedError: failed to baz',
|
|
|
|
])
|
2021-12-16 04:04:32 -05:00
|
|
|
expect(OError.getFullInfo(error)).to.eql({
|
2020-04-28 15:40:20 -04:00
|
|
|
bar: 'baz',
|
|
|
|
baz: 'bat',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2020-09-11 05:05:54 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
it('tags errors yielded through callbacks', function (done) {
|
|
|
|
function foo(cb) {
|
|
|
|
setTimeout(() => {
|
|
|
|
cb(new Error('foo error'))
|
|
|
|
}, 10)
|
|
|
|
}
|
2020-09-11 05:05:54 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
function bar(cb) {
|
|
|
|
foo(err => {
|
|
|
|
if (err) {
|
|
|
|
return cb(OError.tag(err, 'failed to bar', { bar: 'baz' }))
|
|
|
|
}
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function baz(cb) {
|
|
|
|
bar(err => {
|
|
|
|
if (err) {
|
|
|
|
return cb(OError.tag(err, 'failed to baz', { baz: 'bat' }))
|
|
|
|
}
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
baz(err => {
|
|
|
|
if (err) {
|
|
|
|
expectError(err, {
|
|
|
|
name: 'Error',
|
|
|
|
klass: Error,
|
|
|
|
message: 'Error: foo error',
|
|
|
|
firstFrameRx: /_onTimeout/,
|
|
|
|
})
|
|
|
|
expectFullStackWithoutStackFramesToEqual(err, [
|
|
|
|
'Error: foo error',
|
|
|
|
'TaggedError: failed to bar',
|
|
|
|
'TaggedError: failed to baz',
|
|
|
|
])
|
|
|
|
expect(OError.getFullInfo(err)).to.eql({
|
|
|
|
bar: 'baz',
|
|
|
|
baz: 'bat',
|
|
|
|
})
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
expect.fail('should have yielded an error')
|
|
|
|
})
|
2020-09-11 05:05:54 -04:00
|
|
|
})
|
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
it('is not included in the stack trace if using capture', function () {
|
|
|
|
if (!Error.captureStackTrace) return this.skip()
|
2020-09-11 05:05:54 -04:00
|
|
|
const err = new Error('test error')
|
|
|
|
OError.tag(err, 'test message')
|
|
|
|
const stack = OError.getFullStack(err)
|
2021-12-16 04:04:32 -05:00
|
|
|
expect(stack).to.match(/TaggedError: test message\n\s+at/)
|
|
|
|
expect(stack).to.not.match(/TaggedError: test message\n\s+at [\w.]*tag/)
|
2020-09-11 05:05:54 -04:00
|
|
|
})
|
2020-09-17 10:17:44 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
describe('without Error.captureStackTrace', function () {
|
|
|
|
/* eslint-disable mocha/no-hooks-for-single-case */
|
|
|
|
before(function () {
|
|
|
|
this.originalCaptureStackTrace = Error.captureStackTrace
|
|
|
|
Error.captureStackTrace = null
|
|
|
|
})
|
|
|
|
after(function () {
|
|
|
|
Error.captureStackTrace = this.originalCaptureStackTrace
|
|
|
|
})
|
2020-09-17 10:17:44 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
it('still captures a stack trace, albeit including itself', function () {
|
|
|
|
const err = new Error('test error')
|
|
|
|
OError.tag(err, 'test message')
|
|
|
|
expectFullStackWithoutStackFramesToEqual(err, [
|
|
|
|
'Error: test error',
|
|
|
|
'TaggedError: test message',
|
|
|
|
])
|
|
|
|
const stack = OError.getFullStack(err)
|
|
|
|
expect(stack).to.match(/TaggedError: test message\n\s+at [\w.]*tag/)
|
|
|
|
})
|
2020-09-17 10:17:44 -04:00
|
|
|
})
|
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
describe('with limit on the number of tags', function () {
|
|
|
|
before(function () {
|
|
|
|
this.originalMaxTags = OError.maxTags
|
|
|
|
OError.maxTags = 3
|
|
|
|
})
|
|
|
|
after(function () {
|
|
|
|
OError.maxTags = this.originalMaxTags
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not tag more than that', function () {
|
|
|
|
const err = new Error('test error')
|
|
|
|
OError.tag(err, 'test message 1')
|
|
|
|
OError.tag(err, 'test message 2')
|
|
|
|
OError.tag(err, 'test message 3')
|
|
|
|
OError.tag(err, 'test message 4')
|
|
|
|
OError.tag(err, 'test message 5')
|
2020-09-17 10:17:44 -04:00
|
|
|
expectFullStackWithoutStackFramesToEqual(err, [
|
2021-12-16 04:04:32 -05:00
|
|
|
'Error: test error',
|
|
|
|
'TaggedError: test message 1',
|
2020-09-17 10:17:44 -04:00
|
|
|
'TaggedError: ... dropped tags',
|
2021-12-16 04:04:32 -05:00
|
|
|
'TaggedError: test message 4',
|
|
|
|
'TaggedError: test message 5',
|
2020-09-17 10:17:44 -04:00
|
|
|
])
|
2021-12-16 04:04:32 -05:00
|
|
|
})
|
2020-09-17 10:17:44 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
it('should handle deep recursion', async function () {
|
|
|
|
async function recursiveAdd(n) {
|
|
|
|
try {
|
|
|
|
if (n === 0) throw new Error('deep error')
|
|
|
|
const result = await recursiveAdd(n - 1)
|
|
|
|
return result + 1
|
|
|
|
} catch (err) {
|
|
|
|
throw OError.tag(err, `at level ${n}`)
|
|
|
|
}
|
|
|
|
}
|
2020-09-17 10:17:44 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
try {
|
|
|
|
await recursiveAdd(10)
|
|
|
|
} catch (err) {
|
2020-09-17 10:17:44 -04:00
|
|
|
expectFullStackWithoutStackFramesToEqual(err, [
|
2021-12-16 04:04:32 -05:00
|
|
|
'Error: deep error',
|
|
|
|
'TaggedError: at level 0',
|
2020-09-17 10:17:44 -04:00
|
|
|
'TaggedError: ... dropped tags',
|
2021-12-16 04:04:32 -05:00
|
|
|
'TaggedError: at level 9',
|
|
|
|
'TaggedError: at level 10',
|
2020-09-17 10:17:44 -04:00
|
|
|
])
|
2021-12-16 04:04:32 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should handle a singleton error', function (done) {
|
|
|
|
const err = new Error('singleton error')
|
|
|
|
|
|
|
|
function endpoint(callback) {
|
|
|
|
helper(err => callback(err && OError.tag(err, 'in endpoint')))
|
|
|
|
}
|
|
|
|
|
|
|
|
function helper(callback) {
|
|
|
|
libraryFunction(err => callback(err && OError.tag(err, 'in helper')))
|
|
|
|
}
|
|
|
|
|
|
|
|
function libraryFunction(callback) {
|
|
|
|
callback(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint(() => {
|
|
|
|
endpoint(err => {
|
|
|
|
expect(err).to.exist
|
|
|
|
expectFullStackWithoutStackFramesToEqual(err, [
|
|
|
|
'Error: singleton error',
|
|
|
|
'TaggedError: in helper',
|
|
|
|
'TaggedError: ... dropped tags',
|
|
|
|
'TaggedError: in helper',
|
|
|
|
'TaggedError: in endpoint',
|
|
|
|
])
|
|
|
|
done()
|
|
|
|
})
|
2020-09-17 10:17:44 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2019-07-01 06:34:15 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
describe('OError.getFullInfo', function () {
|
|
|
|
it('works when given null', function () {
|
|
|
|
expect(OError.getFullInfo(null)).to.deep.equal({})
|
|
|
|
})
|
2020-09-11 05:05:54 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
it('works on a normal error', function () {
|
|
|
|
const err = new Error('foo')
|
|
|
|
expect(OError.getFullInfo(err)).to.deep.equal({})
|
|
|
|
})
|
2019-07-01 06:34:15 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
it('works on an error with tags', function () {
|
|
|
|
const err = OError.tag(new Error('foo'), 'bar', { userId: 123 })
|
|
|
|
expect(OError.getFullInfo(err)).to.deep.equal({ userId: 123 })
|
|
|
|
})
|
2019-07-01 06:34:15 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
it('merges info from an error and its tags', function () {
|
|
|
|
const err = new OError('foo').withInfo({ projectId: 456 })
|
|
|
|
OError.tag(err, 'failed to foo', { userId: 123 })
|
|
|
|
expect(OError.getFullInfo(err)).to.deep.equal({
|
|
|
|
projectId: 456,
|
|
|
|
userId: 123,
|
|
|
|
})
|
2020-04-28 15:40:20 -04:00
|
|
|
})
|
2019-07-01 06:34:15 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
it('merges info from a cause', function () {
|
|
|
|
const err1 = new Error('foo')
|
|
|
|
const err2 = new Error('bar')
|
|
|
|
err1.cause = err2
|
|
|
|
err2.info = { userId: 123 }
|
|
|
|
expect(OError.getFullInfo(err1)).to.deep.equal({ userId: 123 })
|
|
|
|
})
|
2020-12-02 06:44:17 -05:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
it('merges info from a nested cause', function () {
|
|
|
|
const err1 = new Error('foo')
|
|
|
|
const err2 = new Error('bar')
|
|
|
|
const err3 = new Error('baz')
|
|
|
|
err1.cause = err2
|
|
|
|
err2.info = { userId: 123 }
|
|
|
|
err2.cause = err3
|
|
|
|
err3.info = { foo: 42 }
|
|
|
|
expect(OError.getFullInfo(err1)).to.deep.equal({
|
|
|
|
userId: 123,
|
|
|
|
foo: 42,
|
|
|
|
})
|
2020-12-02 06:44:17 -05:00
|
|
|
})
|
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
it('merges info from cause with duplicate keys', function () {
|
|
|
|
const err1 = new Error('foo')
|
|
|
|
const err2 = new Error('bar')
|
|
|
|
err1.info = { userId: 42, foo: 1337 }
|
|
|
|
err1.cause = err2
|
|
|
|
err2.info = { userId: 1 }
|
|
|
|
expect(OError.getFullInfo(err1)).to.deep.equal({
|
|
|
|
userId: 42,
|
|
|
|
foo: 1337,
|
|
|
|
})
|
2020-12-02 06:44:17 -05:00
|
|
|
})
|
2019-07-01 06:34:15 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
it('merges info from tags with duplicate keys', function () {
|
|
|
|
const err1 = OError.tag(new Error('foo'), 'bar', { userId: 123 })
|
|
|
|
const err2 = OError.tag(err1, 'bat', { userId: 456 })
|
|
|
|
expect(OError.getFullInfo(err2)).to.deep.equal({ userId: 456 })
|
|
|
|
})
|
2019-07-01 06:34:15 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
it('works on an error with .info set to a string', function () {
|
|
|
|
const err = new Error('foo')
|
|
|
|
err.info = 'test'
|
|
|
|
expect(OError.getFullInfo(err)).to.deep.equal({})
|
|
|
|
})
|
2019-07-01 06:34:15 -04:00
|
|
|
})
|
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
describe('OError.getFullStack', function () {
|
|
|
|
it('works when given null', function () {
|
|
|
|
expect(OError.getFullStack(null)).to.equal('')
|
|
|
|
})
|
2020-09-11 05:05:54 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
it('works on a normal error', function () {
|
|
|
|
const err = new Error('foo')
|
|
|
|
const fullStack = OError.getFullStack(err)
|
|
|
|
expect(fullStack).to.match(/^Error: foo$/m)
|
|
|
|
expect(fullStack).to.match(/^\s+at /m)
|
|
|
|
})
|
2019-07-01 06:34:15 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
it('works on an error with a cause', function () {
|
|
|
|
const err1 = new Error('foo')
|
|
|
|
const err2 = new Error('bar')
|
|
|
|
err1.cause = err2
|
2019-07-01 06:34:15 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
const fullStack = OError.getFullStack(err1)
|
|
|
|
expect(fullStack).to.match(/^Error: foo$/m)
|
|
|
|
expect(fullStack).to.match(/^\s+at /m)
|
|
|
|
expect(fullStack).to.match(/^caused by:\n\s+Error: bar$/m)
|
|
|
|
})
|
2019-07-01 06:34:15 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
it('works on both tags and causes', async function () {
|
|
|
|
// Here's the actual error.
|
|
|
|
function tryToFoo() {
|
|
|
|
try {
|
|
|
|
throw Error('foo')
|
|
|
|
} catch (error) {
|
|
|
|
throw OError.tag(error, 'failed to foo', { foo: 1 })
|
|
|
|
}
|
2020-04-28 15:40:20 -04:00
|
|
|
}
|
2019-07-01 06:34:15 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
// Inside another function that wraps it.
|
|
|
|
function tryToBar() {
|
|
|
|
try {
|
|
|
|
tryToFoo()
|
|
|
|
} catch (error) {
|
|
|
|
throw new OError('failed to bar').withCause(error)
|
|
|
|
}
|
2020-04-28 15:40:20 -04:00
|
|
|
}
|
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
// And it is in another try.
|
2020-04-28 15:40:20 -04:00
|
|
|
try {
|
2021-12-16 04:04:32 -05:00
|
|
|
try {
|
|
|
|
tryToBar()
|
|
|
|
expect.fail('should have thrown')
|
|
|
|
} catch (error) {
|
|
|
|
throw OError.tag(error, 'failed to bat', { bat: 1 })
|
|
|
|
}
|
2020-04-28 15:40:20 -04:00
|
|
|
} catch (error) {
|
2021-12-16 04:04:32 -05:00
|
|
|
// We catch the wrapping error.
|
|
|
|
expectError(error, {
|
|
|
|
name: 'OError',
|
|
|
|
klass: OError,
|
|
|
|
message: 'OError: failed to bar',
|
|
|
|
firstFrameRx: /tryToBar/,
|
|
|
|
})
|
2020-04-28 15:40:20 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
// But the stack contains all of the errors and tags.
|
|
|
|
expectFullStackWithoutStackFramesToEqual(error, [
|
|
|
|
'OError: failed to bar',
|
|
|
|
'TaggedError: failed to bat',
|
|
|
|
'caused by:',
|
|
|
|
' Error: foo',
|
|
|
|
' TaggedError: failed to foo',
|
|
|
|
])
|
|
|
|
|
|
|
|
// The info from the wrapped cause should be picked up for logging.
|
|
|
|
expect(OError.getFullInfo(error)).to.eql({ bat: 1, foo: 1 })
|
|
|
|
|
|
|
|
// But it should still be recorded.
|
|
|
|
expect(OError.getFullInfo(error.cause)).to.eql({ foo: 1 })
|
|
|
|
}
|
|
|
|
})
|
2019-07-01 06:34:15 -04:00
|
|
|
})
|
|
|
|
})
|