From 35dd93841bc754393baaa9bcaf4f78e90aeaf4cb Mon Sep 17 00:00:00 2001 From: Julian Dax Date: Sat, 18 Mar 2023 14:13:37 +0100 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/46840 Reviewed-By: Antoine du Hamel Reviewed-By: Luigi Pinca Reviewed-By: Ruben Bridgewater --- doc/api/util.md | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/doc/api/util.md b/doc/api/util.md index a0554a40b2ae8c..caf26dcffcf7fe 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -2515,12 +2515,43 @@ 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 are also native errors: + +```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 were not +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 ``` ### `util.types.isNumberObject(value)` @@ -3335,10 +3366,12 @@ util.log('Timestamped message.'); [`util.types.isNativeError()`]: #utiltypesisnativeerrorvalue [`util.types.isSharedArrayBuffer()`]: #utiltypesissharedarraybuffervalue [async function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function +[built-in `Error` type]: https://tc39.es/ecma262/#sec-error-objects [compare function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Parameters [constructor]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor [default sort]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort [global symbol registry]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for [list of deprecated APIS]: deprecations.md#list-of-deprecated-apis +[realm]: https://tc39.es/ecma262/#realm [semantically incompatible]: https://github.com/nodejs/node/issues/4179 [util.inspect.custom]: #utilinspectcustom