@@ -2515,12 +2515,43 @@ added: v10.0.0
2515
2515
* ` value` {any}
2516
2516
* Returns: {boolean}
2517
2517
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][].
2519
2520
2520
2521
` ` ` 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
2524
2555
` ` `
2525
2556
2526
2557
### ` util .types .isNumberObject (value)`
@@ -3335,10 +3366,12 @@ util.log('Timestamped message.');
3335
3366
[` util .types .isNativeError ()` ]: #utiltypesisnativeerrorvalue
3336
3367
[` util .types .isSharedArrayBuffer ()` ]: #utiltypesissharedarraybuffervalue
3337
3368
[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
3338
3370
[compare function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Parameters
3339
3371
[constructor]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor
3340
3372
[default sort]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
3341
3373
[global symbol registry]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for
3342
3374
[list of deprecated APIS]: deprecations.md#list-of-deprecated-apis
3375
+ [realm]: https://tc39.es/ecma262/#realm
3343
3376
[semantically incompatible]: https://github.com/nodejs/node/issues/4179
3344
3377
[util.inspect.custom]: #utilinspectcustom
0 commit comments