Skip to content

Commit

Permalink
Add error causes
Browse files Browse the repository at this point in the history
If an error is caused by a specific value, that value is used as the
error cause.
  • Loading branch information
remcohaszing committed Apr 5, 2024
1 parent b588f36 commit 2414898
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/estree-util-value-to-estree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export function valueToEstree(value: unknown, options: Options = {}): Expression
*/
function analyze(val: unknown): undefined {
if (typeof val === 'function') {
throw new TypeError(`Unsupported value: ${val}`)
throw new TypeError(`Unsupported value: ${val}`, { cause: val })
}

if (typeof val !== 'object') {
Expand All @@ -272,7 +272,7 @@ export function valueToEstree(value: unknown, options: Options = {}): Expression
}
if (stack.includes(val)) {
if (!options.preserveReferences) {
throw new Error(`Found recursive value: ${val}`)
throw new Error(`Found circular reference: ${val}`, { cause: val })
}
const parent = stack.at(-1)!
const parentContext = collectedContexts.get(parent)!
Expand Down Expand Up @@ -315,7 +315,7 @@ export function valueToEstree(value: unknown, options: Options = {}): Expression
analyze((val as Record<string | symbol, unknown>)[key])
}
} else {
throw new TypeError(`Unsupported value: ${val}`)
throw new TypeError(`Unsupported value: ${val}`, { cause: val })
}
stack.pop()
}
Expand Down Expand Up @@ -367,7 +367,7 @@ export function valueToEstree(value: unknown, options: Options = {}): Expression
return methodCall(identifier('Symbol'), 'for', [literal(val.description)])
}

throw new TypeError(`Only global symbols are supported, got: ${String(val)}`)
throw new TypeError(`Only global symbols are supported, got: ${String(val)}`, { cause: val })
}

const context = collectedContexts.get(val)
Expand Down
50 changes: 45 additions & 5 deletions src/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as assert from 'node:assert/strict'
import assert from 'node:assert/strict'
import { test } from 'node:test'

import { generate } from 'astring'
Expand Down Expand Up @@ -34,18 +34,58 @@ testFixturesDirectory({
})

test('throw for local symbols', () => {
const symbol = Symbol('local')
assert.throws(
() => valueToEstree(Symbol('local')),
new TypeError('Only global symbols are supported, got: Symbol(local)')
() => valueToEstree(symbol),
(error) => {
assert(error instanceof TypeError)
assert.equal(error.message, 'Only global symbols are supported, got: Symbol(local)')
assert.equal(error.cause, symbol)
return true
}
)
})

test('throw for unsupported values', () => {
assert.throws(() => valueToEstree(() => null), new TypeError('Unsupported value: () => null'))
const fn = (): null => null
assert.throws(
() => valueToEstree(fn),
(error) => {
assert(error instanceof TypeError)
assert.equal(error.message, 'Unsupported value: () => null')
assert.equal(error.cause, fn)
return true
}
)

class A {
a = ''
}
assert.throws(() => valueToEstree(new A()), new TypeError('Unsupported value: [object Object]'))

const a = new A()
assert.throws(
() => valueToEstree(a),
(error) => {
assert(error instanceof TypeError)
assert.equal(error.message, 'Unsupported value: [object Object]')
assert.equal(error.cause, a)
return true
}
)
})

test('throw for cyclic references', () => {
const object: Record<string, unknown> = {}
object.reference = object
assert.throws(
() => valueToEstree(object),
(error) => {
assert(error instanceof Error)
assert.equal(error.message, 'Found circular reference: [object Object]')
assert.equal(error.cause, object)
return true
}
)
})

test('transform to json on unsupported values w/ `instanceAsObject`', () => {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"outDir": "dist",
"sourceMap": true,
"strict": true,
"target": "es2020"
"target": "es2022"
}
}

0 comments on commit 2414898

Please sign in to comment.