diff --git a/src/error.ts b/src/error.ts index 0da5e40..f22be36 100644 --- a/src/error.ts +++ b/src/error.ts @@ -12,6 +12,7 @@ const errorKeys = [ 'columnNumber', 'number', 'description', + 'cause', ] export default function inspectObject(error: Error, options: Options) { @@ -26,6 +27,11 @@ export default function inspectObject(error: Error, options: Options) { } message = message ? `: ${message}` : '' options.truncate -= message.length + 5 + options.seen = options.seen || [] + if (options.seen.indexOf(error) >= 0) { + return '[Circular]' + } + options.seen.push(error) const propertyContents = inspectList( properties.map(key => [key, error[key as keyof typeof error]]), options, diff --git a/test/errors.js b/test/errors.js index e5d8430..565687f 100644 --- a/test/errors.js +++ b/test/errors.js @@ -33,5 +33,19 @@ describe('errors', () => { err.message = { code: 404 } expect(inspect(err)).to.equal('Error { message: { code: 404 } }') }) + + it('detects circular references', () => { + const err = new Error('message') + err.fluff = err + expect(inspect(err)).to.equal('Error: message { fluff: [Circular] }') + }) + }) + + describe('ignores built in properties', () => { + it('does not add property to output', () => { + const err = new Error('message') + err.cause = new Error('i caused you') + expect(inspect(err)).to.equal('Error: message') + }) }) })