Skip to content

Commit 35dd938

Browse files
authored
doc: improve documentation for util.types.isNativeError()
Makes clear what a native error is by linking the spec. Explains that `instanceof Error` and util.types.isNativeError() are not equivalent. Give examples for objects that are `instance of Error` but not native errors and vice versa. Recommends checking for both if one wants to find out if something is an error. PR-URL: #46840 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 9ce2f4f commit 35dd938

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

doc/api/util.md

+37-4
Original file line numberDiff line numberDiff line change
@@ -2515,12 +2515,43 @@ added: v10.0.0
25152515
* `value` {any}
25162516
* Returns: {boolean}
25172517
2518-
Returns `true` if the value is an instance of a built-in [`Error`][] type.
2518+
Returns `true` if the value was returned by the constructor of a
2519+
[built-in `Error` type][].
25192520
25202521
```js
2521-
util.types.isNativeError(new Error()); // Returns true
2522-
util.types.isNativeError(new TypeError()); // Returns true
2523-
util.types.isNativeError(new RangeError()); // Returns true
2522+
console.log(util.types.isNativeError(new Error())); // true
2523+
console.log(util.types.isNativeError(new TypeError())); // true
2524+
console.log(util.types.isNativeError(new RangeError())); // true
2525+
```
2526+
2527+
Subclasses of the native error types are also native errors:
2528+
2529+
```js
2530+
class MyError extends Error {}
2531+
console.log(util.types.isNativeError(new MyError())); // true
2532+
```
2533+
2534+
A value being `instanceof` a native error class is not equivalent to `isNativeError()`
2535+
returning `true` for that value. `isNativeError()` returns `true` for errors
2536+
which come from a different [realm][] while `instanceof Error` returns `false`
2537+
for these errors:
2538+
2539+
```js
2540+
const vm = require('node:vm');
2541+
const context = vm.createContext({});
2542+
const myError = vm.runInContext('new Error', context);
2543+
console.log(util.types.isNativeError(myError)); // true
2544+
console.log(myError instanceof Error); // false
2545+
```
2546+
2547+
Conversely, `isNativeError()` returns `false` for all objects which were not
2548+
returned by the constructor of a native error. That includes values
2549+
which are `instanceof` native errors:
2550+
2551+
```js
2552+
const myError = { __proto__: Error.prototype };
2553+
console.log(util.types.isNativeError(myError)); // false
2554+
console.log(myError instanceof Error); // true
25242555
```
25252556
25262557
### `util.types.isNumberObject(value)`
@@ -3335,10 +3366,12 @@ util.log('Timestamped message.');
33353366
[`util.types.isNativeError()`]: #utiltypesisnativeerrorvalue
33363367
[`util.types.isSharedArrayBuffer()`]: #utiltypesissharedarraybuffervalue
33373368
[async function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
3369+
[built-in `Error` type]: https://tc39.es/ecma262/#sec-error-objects
33383370
[compare function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Parameters
33393371
[constructor]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor
33403372
[default sort]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
33413373
[global symbol registry]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for
33423374
[list of deprecated APIS]: deprecations.md#list-of-deprecated-apis
3375+
[realm]: https://tc39.es/ecma262/#realm
33433376
[semantically incompatible]: https://github.com/nodejs/node/issues/4179
33443377
[util.inspect.custom]: #utilinspectcustom

0 commit comments

Comments
 (0)