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

doc: improve documentation for util.types.isNativeError() #46840

Merged
merged 12 commits into from
Mar 18, 2023
42 changes: 38 additions & 4 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -2515,12 +2515,44 @@ added: v10.0.0
* `value` {any}
* Returns: {boolean}

Returns `true` if the value is an instance of a built-in [`Error`][] type.
Returns `true` if the value was returned by the constructor of a
[built-in `Error` type][].

```js
util.types.isNativeError(new Error()); // Returns true
util.types.isNativeError(new TypeError()); // Returns true
util.types.isNativeError(new RangeError()); // Returns true
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 will use the return value of the native
error constructor as the new `this` and are therefore also native errors:
brodo marked this conversation as resolved.
Show resolved Hide resolved

```js
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:

```js
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 where not
brodo marked this conversation as resolved.
Show resolved Hide resolved
returned by the constructor of a native error. That includes values
which are `instanceof` native errors:

```js
const myError = { __proto__: Error.prototype };
console.log(util.types.isNativeError(myError)); // false
console.log(myError instanceof Error)); // true
brodo marked this conversation as resolved.
Show resolved Hide resolved
```

### `util.types.isNumberObject(value)`
Expand Down Expand Up @@ -3287,6 +3319,8 @@ util.log('Timestamped message.');
[Customizing `util.inspect` colors]: #customizing-utilinspect-colors
[Internationalization]: intl.md
[Module Namespace Object]: https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects
[realm]: https://tc39.es/ecma262/#realm
[built-in `Error` type]: https://tc39.es/ecma262/#sec-error-objects
brodo marked this conversation as resolved.
Show resolved Hide resolved
[WHATWG Encoding Standard]: https://encoding.spec.whatwg.org/
[`'uncaughtException'`]: process.md#event-uncaughtexception
[`'warning'`]: process.md#event-warning
Expand Down