Skip to content

Commit 0da1632

Browse files
juanarboladuh95
authored andcommitted
src,lib: introduce util.getSystemErrorMessage(err)
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 <soyjuanarbol@gmail.com> PR-URL: #54075 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent 1b63a82 commit 0da1632

File tree

5 files changed

+56
-2
lines changed

5 files changed

+56
-2
lines changed

doc/api/util.md

+20
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,26 @@ fs.access('file/that/does/not/exist', (err) => {
463463
});
464464
```
465465

466+
## `util.getSystemErrorMessage(err)`
467+
468+
<!-- YAML
469+
added: REPLACEME
470+
-->
471+
472+
* `err` {number}
473+
* Returns: {string}
474+
475+
Returns the string message for a numeric error code that comes from a Node.js
476+
API.
477+
The mapping between error codes and string messages is platform-dependent.
478+
479+
```js
480+
fs.access('file/that/does/not/exist', (err) => {
481+
const name = util.getSystemErrorMessage(err.errno);
482+
console.error(name); // no such file or directory
483+
});
484+
```
485+
466486
## `util.inherits(constructor, superConstructor)`
467487

468488
<!-- YAML

lib/internal/util.js

+5
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,10 @@ function getCWDURL() {
386386
return cachedURL;
387387
}
388388

389+
function getSystemErrorMessage(err) {
390+
return lazyUv().getErrorMessage(err);
391+
}
392+
389393
function getSystemErrorName(err) {
390394
const entry = uvErrmapGet(err);
391395
return entry ? entry[0] : `Unknown system error ${err}`;
@@ -880,6 +884,7 @@ module.exports = {
880884
getStructuredStack,
881885
getSystemErrorMap,
882886
getSystemErrorName,
887+
getSystemErrorMessage,
883888
guessHandleType,
884889
isError,
885890
isUnderNodeModules,

lib/util.js

+14
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ const {
8181
deprecate,
8282
getSystemErrorMap,
8383
getSystemErrorName: internalErrorName,
84+
getSystemErrorMessage: internalErrorMessage,
8485
promisify,
8586
defineLazyProperties,
8687
} = require('internal/util');
@@ -269,6 +270,18 @@ function callbackify(original) {
269270
return callbackified;
270271
}
271272

273+
/**
274+
* @param {number} err
275+
* @returns {string}
276+
*/
277+
function getSystemErrorMessage(err) {
278+
validateNumber(err, 'err');
279+
if (err >= 0 || !NumberIsSafeInteger(err)) {
280+
throw new ERR_OUT_OF_RANGE('err', 'a negative integer', err);
281+
}
282+
return internalErrorMessage(err);
283+
}
284+
272285
/**
273286
* @param {number} err
274287
* @returns {string}
@@ -343,6 +356,7 @@ module.exports = {
343356
getCallSite,
344357
getSystemErrorMap,
345358
getSystemErrorName,
359+
getSystemErrorMessage,
346360
inherits,
347361
inspect,
348362
isArray: deprecate(ArrayIsArray,

src/uv.cc

+12-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ using v8::ReadOnly;
5959
using v8::String;
6060
using v8::Value;
6161

62+
void GetErrMessage(const FunctionCallbackInfo<Value>& args) {
63+
Environment* env = Environment::GetCurrent(args);
64+
int err = args[0].As<v8::Int32>()->Value();
65+
CHECK_LT(err, 0);
66+
char message[50];
67+
uv_strerror_r(err, message, sizeof(message));
68+
args.GetReturnValue().Set(OneByteString(env->isolate(), message));
69+
}
70+
6271
void ErrName(const FunctionCallbackInfo<Value>& args) {
6372
Environment* env = Environment::GetCurrent(args);
6473
if (env->options()->pending_deprecation && env->EmitErrNameWarning()) {
@@ -70,8 +79,7 @@ void ErrName(const FunctionCallbackInfo<Value>& args) {
7079
"DEP0119").IsNothing())
7180
return;
7281
}
73-
int err;
74-
if (!args[0]->Int32Value(env->context()).To(&err)) return;
82+
int err = args[0].As<v8::Int32>()->Value();
7583
CHECK_LT(err, 0);
7684
char name[50];
7785
uv_err_name_r(err, name, sizeof(name));
@@ -128,11 +136,13 @@ void Initialize(Local<Object> target,
128136
}
129137

130138
SetMethod(context, target, "getErrorMap", GetErrMap);
139+
SetMethod(context, target, "getErrorMessage", GetErrMessage);
131140
}
132141

133142
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
134143
registry->Register(ErrName);
135144
registry->Register(GetErrMap);
145+
registry->Register(GetErrMessage);
136146
}
137147
} // namespace uv
138148
} // namespace node

test/parallel/test-uv-errno.js

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const common = require('../common');
55
const assert = require('assert');
66
const {
77
getSystemErrorName,
8+
getSystemErrorMessage,
89
_errnoException
910
} = require('util');
1011

@@ -13,6 +14,7 @@ const uv = internalBinding('uv');
1314
const keys = Object.keys(uv);
1415

1516
assert.strictEqual(uv.errname(-111111), 'Unknown system error -111111');
17+
assert.strictEqual(uv.getErrorMessage(-111111), 'Unknown system error -111111');
1618

1719
keys.forEach((key) => {
1820
if (!key.startsWith('UV_'))
@@ -21,6 +23,8 @@ keys.forEach((key) => {
2123
const err = _errnoException(uv[key], 'test');
2224
const name = uv.errname(uv[key]);
2325
assert.strictEqual(getSystemErrorName(uv[key]), name);
26+
assert.notStrictEqual(getSystemErrorMessage(uv[key]),
27+
`Unknown system error ${key}`);
2428
assert.strictEqual(err.code, name);
2529
assert.strictEqual(err.code, getSystemErrorName(err.errno));
2630
assert.strictEqual(err.message, `test ${name}`);
@@ -53,3 +57,4 @@ function runTest(fn) {
5357

5458
runTest(_errnoException);
5559
runTest(getSystemErrorName);
60+
runTest(getSystemErrorMessage);

0 commit comments

Comments
 (0)