From 28fa906ec1f9a20b5953576cc26d4e4a7ba4a27a Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 17 Jan 2018 03:21:16 +0800 Subject: [PATCH] util: implement util.getSystemErrorName() Reimplement uv.errname() as internal/util.getSystemErrorName() to avoid the memory leaks caused by unknown error codes and avoid calling into C++ for the error names. Also expose it as a public API for external use. Backport-PR-URL: https://github.com/nodejs/node/pull/18916 PR-URL: https://github.com/nodejs/node/pull/18186 Refs: http://docs.libuv.org/en/v1.x/errors.html#c.uv_err_name Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Luigi Pinca Reviewed-By: Ruben Bridgewater --- doc/api/util.md | 22 ++++++++++++++++++- lib/child_process.js | 7 +++--- lib/internal/util.js | 13 ++++++++++- lib/util.js | 10 +++------ src/uv.cc | 2 ++ test/parallel/test-child-process-execfile.js | 5 +++-- .../parallel/test-net-server-listen-handle.js | 7 +++--- test/parallel/test-uv-errno.js | 14 +++++++----- test/sequential/test-async-wrap-getasyncid.js | 3 ++- 9 files changed, 60 insertions(+), 23 deletions(-) diff --git a/doc/api/util.md b/doc/api/util.md index db56da1618e6f7..5ad1e5cdb56225 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -224,6 +224,25 @@ without any formatting. util.format('%% %s'); // '%% %s' ``` +## util.getSystemErrorName(err) + + +* `err` {number} +* Returns: {string} + +Returns the string name 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.getSystemErrorName(err.errno); + console.error(name); // ENOENT +}); +``` + ## util.inherits(constructor, superConstructor)