From 017013af8b414c728b541899b1156d90309b40a2 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Mon, 25 Dec 2017 21:36:15 +0800 Subject: [PATCH 1/5] src: expose uv.errmap to binding Add a errno -> [error code, uv error message] map to the uv binding so the error message can be assembled in the JS layer. PR-URL: https://github.com/nodejs/node/pull/17338 Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- src/uv.cc | 25 +++++++++++++++++++++-- test/parallel/test-uv-binding-constant.js | 12 +++++------ test/parallel/test-uv-errno.js | 2 +- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/uv.cc b/src/uv.cc index f70da1baae5deb..3c115cf05b5a3a 100644 --- a/src/uv.cc +++ b/src/uv.cc @@ -27,10 +27,15 @@ namespace node { namespace { +using v8::Array; using v8::Context; using v8::FunctionCallbackInfo; +using v8::Integer; +using v8::Isolate; using v8::Local; +using v8::Map; using v8::Object; +using v8::String; using v8::Value; @@ -47,14 +52,30 @@ void InitializeUV(Local target, Local unused, Local context) { Environment* env = Environment::GetCurrent(context); - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "errname"), + Isolate* isolate = env->isolate(); + target->Set(FIXED_ONE_BYTE_STRING(isolate, "errname"), env->NewFunctionTemplate(ErrName)->GetFunction()); #define V(name, _) NODE_DEFINE_CONSTANT(target, UV_##name); UV_ERRNO_MAP(V) #undef V -} + Local err_map = Map::New(isolate); + +#define V(name, msg) do { \ + Local arr = Array::New(isolate, 2); \ + arr->Set(0, OneByteString(isolate, #name)); \ + arr->Set(1, OneByteString(isolate, msg)); \ + err_map->Set(context, \ + Integer::New(isolate, UV_##name), \ + arr).ToLocalChecked(); \ +} while (0); + UV_ERRNO_MAP(V) +#undef V + + target->Set(context, FIXED_ONE_BYTE_STRING(isolate, "errmap"), + err_map).FromJust(); +} } // anonymous namespace } // namespace node diff --git a/test/parallel/test-uv-binding-constant.js b/test/parallel/test-uv-binding-constant.js index cad08575ca92eb..beae7672db12f5 100644 --- a/test/parallel/test-uv-binding-constant.js +++ b/test/parallel/test-uv-binding-constant.js @@ -9,10 +9,10 @@ const uv = process.binding('uv'); const keys = Object.keys(uv); keys.forEach((key) => { - if (key === 'errname') - return; // skip this - const val = uv[key]; - assert.throws(() => uv[key] = 1, - /^TypeError: Cannot assign to read only property/); - assert.strictEqual(uv[key], val); + if (key.startsWith('UV_')) { + const val = uv[key]; + assert.throws(() => uv[key] = 1, + /^TypeError: Cannot assign to read only property/); + assert.strictEqual(uv[key], val); + } }); diff --git a/test/parallel/test-uv-errno.js b/test/parallel/test-uv-errno.js index d5ae7548a040d7..223be9058050d6 100644 --- a/test/parallel/test-uv-errno.js +++ b/test/parallel/test-uv-errno.js @@ -8,7 +8,7 @@ const uv = process.binding('uv'); const keys = Object.keys(uv); keys.forEach((key) => { - if (key === 'errname') + if (!key.startsWith('UV_')) return; assert.doesNotThrow(() => { From 3b18daec151e1de77d91f41fa7189468f2ecb854 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 17 Jan 2018 03:21:16 +0800 Subject: [PATCH 2/5] 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. 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 00d04df8f850ee..ac2bef976a57fb 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -228,6 +228,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)