From cf6d05950d991b0cbeb06aa10d5bcf79c485c81f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Arboleda?= Date: Sat, 27 Jul 2024 22:50:55 -0500 Subject: [PATCH] src,lib: introduce `util.getSystemErrorMessage(err)` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch adds a new utility function which provides human-readable string description of the given system error code. Signed-off-by: Juan José Arboleda --- doc/api/util.md | 21 +++++++++++++++++++++ lib/internal/util.js | 5 +++++ lib/util.js | 14 ++++++++++++++ src/uv.cc | 12 ++++++++++++ test/parallel/test-uv-errno.js | 5 +++++ 5 files changed, 57 insertions(+) diff --git a/doc/api/util.md b/doc/api/util.md index bc7c7da964776d0..26858acd4accc7e 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -406,6 +406,27 @@ fs.access('file/that/does/not/exist', (err) => { }); ``` +## `util.getSystemErrorMessage(err)` + + + +* `err` {number} +* Returns: {string} + +Returns the string message for a numeric error code that comes from a Node.js +API. +The mapping between error codes and error names is platform-dependent. +See [Common System Errors][] for the names of common errors. + +```js +fs.access('file/that/does/not/exist', (err) => { + const name = util.getSystemErrorMessage(err.errno); + console.error(name); // no such file or directory +}); +``` + ## `util.inherits(constructor, superConstructor)`