Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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: #17338
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
joyeecheung committed Feb 22, 2018
commit 017013af8b414c728b541899b1156d90309b40a2
25 changes: 23 additions & 2 deletions src/uv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -47,14 +52,30 @@ void InitializeUV(Local<Object> target,
Local<Value> unused,
Local<Context> 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<Map> err_map = Map::New(isolate);

#define V(name, msg) do { \
Local<Array> 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
Expand Down
12 changes: 6 additions & 6 deletions test/parallel/test-uv-binding-constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
2 changes: 1 addition & 1 deletion test/parallel/test-uv-errno.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down