From 61e52c8bd340efa16db48c8114e14c26ea912319 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9?= Date: Sat, 19 Oct 2024 18:15:16 -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 PR-URL: https://github.com/nodejs/node/pull/54075 Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Yagiz Nizipli --- doc/api/util.md | 20 ++++++++++++++++++++ lib/internal/util.js | 5 +++++ lib/util.js | 14 ++++++++++++++ src/uv.cc | 14 ++++++++++++-- test/parallel/test-uv-errno.js | 5 +++++ 5 files changed, 56 insertions(+), 2 deletions(-) diff --git a/doc/api/util.md b/doc/api/util.md index 8623ab88c9ecfd..3d61e24d148596 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -463,6 +463,26 @@ 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 string messages is platform-dependent. + +```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)`