1+ import isErrorInstance from 'is-error-instance'
12import isPlainObj from 'is-plain-obj'
23
34import { isNonModifiableError } from './modifiable.js'
@@ -12,8 +13,8 @@ export const createError = function (value) {
1213 return objectifyError ( value )
1314 }
1415
15- if ( ! isError ( value ) ) {
16- return handleNonError ( value )
16+ if ( ! isErrorInstance ( value ) ) {
17+ return stringifyError ( value )
1718 }
1819
1920 if ( isInvalidError ( value ) ) {
@@ -32,40 +33,25 @@ const isErrorPlainObj = function (value) {
3233 }
3334}
3435
35- // Unlike `instanceof Error`, this works cross-realm,
36- // e.g. `vm.runInNewContext('Error')`.
37- // Handle hooks exceptions when `value` is a Proxy.
38- const isError = function ( value ) {
39- try {
40- return (
41- objectToString . call ( value ) === '[object Error]' &&
42- ! ( Symbol . toStringTag in value )
43- )
44- } catch {
45- return false
46- }
47- }
48-
49- const handleNonError = function ( value ) {
50- return isProxy ( value ) ? objectifyError ( value ) : stringifyError ( value )
36+ const isInvalidError = function ( value ) {
37+ return (
38+ isProxy ( value ) ||
39+ isNonModifiableError ( value ) ||
40+ hasInvalidConstructor ( value )
41+ )
5142}
5243
5344// Proxies of Errors are converted to non-proxies.
5445// This can only work within the same realm, because the only way to detect
5546// proxies is combining `Object.prototype.toString()` and `instanceof`.
56- // Handle hooks exceptions when `value` is a Proxy.
5747const isProxy = function ( value ) {
5848 try {
59- return value instanceof Error
49+ return objectToString . call ( value ) === '[object Object]'
6050 } catch {
6151 return true
6252 }
6353}
6454
65- const isInvalidError = function ( value ) {
66- return isNonModifiableError ( value ) || hasInvalidConstructor ( value )
67- }
68-
6955// `error.constructor` is often used for type checking, so we ensure it
7056// is normal.
7157const hasInvalidConstructor = function ( error ) {
0 commit comments