function isNativeError
Usage in Deno
import { isNativeError } from "node:util/types";
isNativeError(object: unknown): object is Error
Returns true
if the value was returned by the constructor of a built-in Error
type.
console.log(util.types.isNativeError(new Error())); // true console.log(util.types.isNativeError(new TypeError())); // true console.log(util.types.isNativeError(new RangeError())); // true
Subclasses of the native error types are also native errors:
class MyError extends Error {} console.log(util.types.isNativeError(new MyError())); // true
A value being instanceof
a native error class is not equivalent to isNativeError()
returning true
for that value. isNativeError()
returns true
for errors
which come from a different realm while instanceof Error
returns false
for these errors:
const vm = require('node:vm'); const context = vm.createContext({}); const myError = vm.runInContext('new Error()', context); console.log(util.types.isNativeError(myError)); // true console.log(myError instanceof Error); // false
Conversely, isNativeError()
returns false
for all objects which were not
returned by the constructor of a native error. That includes values
which are instanceof
native errors:
const myError = { __proto__: Error.prototype }; console.log(util.types.isNativeError(myError)); // false console.log(myError instanceof Error); // true
object is Error