Skip to content

Commit 4de4c54

Browse files
joyeecheungMylesBorins
authored andcommitted
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. Backport-PR-URL: #18916 PR-URL: #17338 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 8c93499 commit 4de4c54

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

src/uv.cc

+23-2
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@
2727
namespace node {
2828
namespace {
2929

30+
using v8::Array;
3031
using v8::Context;
3132
using v8::FunctionCallbackInfo;
33+
using v8::Integer;
34+
using v8::Isolate;
3235
using v8::Local;
36+
using v8::Map;
3337
using v8::Object;
38+
using v8::String;
3439
using v8::Value;
3540

3641

@@ -47,14 +52,30 @@ void InitializeUV(Local<Object> target,
4752
Local<Value> unused,
4853
Local<Context> context) {
4954
Environment* env = Environment::GetCurrent(context);
50-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "errname"),
55+
Isolate* isolate = env->isolate();
56+
target->Set(FIXED_ONE_BYTE_STRING(isolate, "errname"),
5157
env->NewFunctionTemplate(ErrName)->GetFunction());
5258

5359
#define V(name, _) NODE_DEFINE_CONSTANT(target, UV_##name);
5460
UV_ERRNO_MAP(V)
5561
#undef V
56-
}
5762

63+
Local<Map> err_map = Map::New(isolate);
64+
65+
#define V(name, msg) do { \
66+
Local<Array> arr = Array::New(isolate, 2); \
67+
arr->Set(0, OneByteString(isolate, #name)); \
68+
arr->Set(1, OneByteString(isolate, msg)); \
69+
err_map->Set(context, \
70+
Integer::New(isolate, UV_##name), \
71+
arr).ToLocalChecked(); \
72+
} while (0);
73+
UV_ERRNO_MAP(V)
74+
#undef V
75+
76+
target->Set(context, FIXED_ONE_BYTE_STRING(isolate, "errmap"),
77+
err_map).FromJust();
78+
}
5879

5980
} // anonymous namespace
6081
} // namespace node

test/parallel/test-uv-binding-constant.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ const uv = process.binding('uv');
99

1010
const keys = Object.keys(uv);
1111
keys.forEach((key) => {
12-
if (key === 'errname')
13-
return; // skip this
14-
const val = uv[key];
15-
assert.throws(() => uv[key] = 1,
16-
/^TypeError: Cannot assign to read only property/);
17-
assert.strictEqual(uv[key], val);
12+
if (key.startsWith('UV_')) {
13+
const val = uv[key];
14+
assert.throws(() => uv[key] = 1,
15+
/^TypeError: Cannot assign to read only property/);
16+
assert.strictEqual(uv[key], val);
17+
}
1818
});

test/parallel/test-uv-errno.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const uv = process.binding('uv');
88
const keys = Object.keys(uv);
99

1010
keys.forEach((key) => {
11-
if (key === 'errname')
11+
if (!key.startsWith('UV_'))
1212
return;
1313

1414
assert.doesNotThrow(() => {

0 commit comments

Comments
 (0)