Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src,lib: introduce util.getSystemErrorMessage(err) #54075

Merged
merged 1 commit into from
Oct 19, 2024
Merged
Changes from all commits
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
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 <[email protected]>
juanarbol committed Oct 19, 2024
commit f42721ba22b73be0f02e12aa81d284ef6f9df048
20 changes: 20 additions & 0 deletions doc/api/util.md
Original file line number Diff line number Diff line change
@@ -463,6 +463,26 @@ fs.access('file/that/does/not/exist', (err) => {
});
```

## `util.getSystemErrorMessage(err)`

<!-- YAML
added: REPLACEME
-->

* `err` {number}
* Returns: {string}

Returns the string message for a numeric error code that comes from a Node.js
API.
The mapping between error codes and string messages is platform-dependent.

```js
fs.access('file/that/does/not/exist', (err) => {
const name = util.getSystemErrorMessage(err.errno);
console.error(name); // no such file or directory
});
```

## `util.inherits(constructor, superConstructor)`

<!-- YAML
5 changes: 5 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
@@ -386,6 +386,10 @@ function getCWDURL() {
return cachedURL;
}

function getSystemErrorMessage(err) {
return lazyUv().getErrorMessage(err);
}

function getSystemErrorName(err) {
const entry = uvErrmapGet(err);
return entry ? entry[0] : `Unknown system error ${err}`;
@@ -880,6 +884,7 @@ module.exports = {
getStructuredStack,
getSystemErrorMap,
getSystemErrorName,
getSystemErrorMessage,
guessHandleType,
isError,
isUnderNodeModules,
14 changes: 14 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -81,6 +81,7 @@ const {
deprecate,
getSystemErrorMap,
getSystemErrorName: internalErrorName,
getSystemErrorMessage: internalErrorMessage,
promisify,
defineLazyProperties,
} = require('internal/util');
@@ -269,6 +270,18 @@ function callbackify(original) {
return callbackified;
}

/**
* @param {number} err
* @returns {string}
*/
function getSystemErrorMessage(err) {
validateNumber(err, 'err');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to call validateNumber since in the next line we check for SafeNumber?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (err >= 0 || !NumberIsSafeInteger(err)) {
throw new ERR_OUT_OF_RANGE('err', 'a negative integer', err);
}
return internalErrorMessage(err);
}

/**
* @param {number} err
* @returns {string}
@@ -343,6 +356,7 @@ module.exports = {
getCallSite,
getSystemErrorMap,
getSystemErrorName,
getSystemErrorMessage,
inherits,
inspect,
isArray: deprecate(ArrayIsArray,
14 changes: 12 additions & 2 deletions src/uv.cc
Original file line number Diff line number Diff line change
@@ -59,6 +59,15 @@ using v8::ReadOnly;
using v8::String;
using v8::Value;

void GetErrMessage(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
int err = args[0].As<v8::Int32>()->Value();
CHECK_LT(err, 0);
char message[50];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 50? Can you add a comment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment?

uv_strerror_r(err, message, sizeof(message));
args.GetReturnValue().Set(OneByteString(env->isolate(), message));
}

void ErrName(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
if (env->options()->pending_deprecation && env->EmitErrNameWarning()) {
@@ -70,8 +79,7 @@ void ErrName(const FunctionCallbackInfo<Value>& args) {
"DEP0119").IsNothing())
return;
}
int err;
if (!args[0]->Int32Value(env->context()).To(&err)) return;
int err = args[0].As<v8::Int32>()->Value();
CHECK_LT(err, 0);
char name[50];
uv_err_name_r(err, name, sizeof(name));
@@ -128,11 +136,13 @@ void Initialize(Local<Object> target,
}

SetMethod(context, target, "getErrorMap", GetErrMap);
SetMethod(context, target, "getErrorMessage", GetErrMessage);
}

void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(ErrName);
registry->Register(GetErrMap);
registry->Register(GetErrMessage);
}
} // namespace uv
} // namespace node
5 changes: 5 additions & 0 deletions test/parallel/test-uv-errno.js
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ const common = require('../common');
const assert = require('assert');
const {
getSystemErrorName,
getSystemErrorMessage,
_errnoException
} = require('util');

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

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

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

runTest(_errnoException);
runTest(getSystemErrorName);
runTest(getSystemErrorMessage);
Loading