Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle circular refs in errors #82

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const errorKeys = [
'columnNumber',
'number',
'description',
'cause',
]

export default function inspectObject(error: Error, options: Options) {
Expand All @@ -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) {
43081j marked this conversation as resolved.
Show resolved Hide resolved
return '[Circular]'
}
options.seen.push(error)
const propertyContents = inspectList(
properties.map(key => [key, error[key as keyof typeof error]]),
options,
Expand Down
14 changes: 14 additions & 0 deletions test/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
})
Loading