Skip to content

Commit

Permalink
Handle Proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Oct 9, 2022
1 parent e6d0936 commit ed74527
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.1.0

- Handle `Proxy`s

# 1.0.0

Initial release
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ iframes or Node.js [`vm`](https://nodejs.org/api/vm.html).

# Example

<!-- eslint-disable fp/no-proxy -->

```js
import isErrorInstance from 'is-error-instance'

Expand All @@ -25,6 +27,17 @@ console.log(isErrorInstance(new AnyOtherError(''))) // true

console.log(isErrorInstance(new DOMException(''))) // true
console.log(isErrorInstance(new DOMError(''))) // true

console.log(isErrorInstance(new Proxy(new Error(''), {}))) // true
console.log(
isErrorInstance(
new Proxy(new Error(''), {
getPrototypeOf() {
throw new Error('')
},
}),
),
) // false
```

# Install
Expand Down
18 changes: 17 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
// Check if a value is an error instance
export default function isErrorInstance(value) {
return value instanceof Error || ERROR_TAGS.has(protoToString.call(value))
return isInstanceOfError(value) || hasErrorTag(value)
}

const isInstanceOfError = function (value) {
try {
return value instanceof Error
} catch {
return false
}
}

const hasErrorTag = function (value) {
try {
return ERROR_TAGS.has(protoToString.call(value))
} catch {
return false
}
}

const ERROR_TAGS = new Set([
Expand Down
46 changes: 42 additions & 4 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import test from 'ava'
import isErrorInstance from 'is-error-instance'
import { each } from 'test-each'

each(
['test', undefined, null, {}, Object.create(null)],
({ title }, nonError) => {
test(`Detects non-errors | ${title}`, (t) => {
t.false(isErrorInstance(nonError))
})
},
)

// eslint-disable-next-line fp/no-class
class ChildError extends Error {}
// eslint-disable-next-line fp/no-mutation
Expand All @@ -27,10 +36,39 @@ each(
)

each(
['test', undefined, null, {}, Object.create(null)],
({ title }, nonError) => {
test(`Detects non-errors | ${title}`, (t) => {
t.false(isErrorInstance(nonError))
[
{
title: 'none',
proxy: {},
isError: true,
},
{
title: 'getPrototypeOf',
proxy: {
getPrototypeOf() {
throw new Error('unsafe')
},
},
isError: false,
},
{
title: 'get',
proxy: {
getPrototypeOf() {
throw new Error('unsafe')
},
get() {
throw new Error('unsafe')
},
},
isError: false,
},
],
({ title }, { proxy, isError }) => {
test(`Detects errors in proxy | ${title}`, (t) => {
// eslint-disable-next-line fp/no-proxy
const error = new Proxy(new Error('test'), proxy)
t.is(isErrorInstance(error), isError)
})
},
)

0 comments on commit ed74527

Please sign in to comment.