-
-
Notifications
You must be signed in to change notification settings - Fork 35.2k
util: implement util.getSystemErrorName() #18186
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -257,6 +257,25 @@ intended as a debugging tool. Some input values can have a significant | |
| performance overhead that can block the event loop. Use this function | ||
| with care and never in a hot code path. | ||
|
|
||
| ## util.getErrorName(code) | ||
| <!-- YAML | ||
| added: REPLACEME | ||
| --> | ||
|
|
||
| * `code` {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.getErrorName(err.errno); | ||
| console.log(name); // ENOENT | ||
| }); | ||
| ``` | ||
|
|
||
| ## util.inherits(constructor, superConstructor) | ||
| <!-- YAML | ||
| added: v0.3.0 | ||
|
|
@@ -1362,6 +1381,7 @@ Deprecated predecessor of `console.log`. | |
| [Customizing `util.inspect` colors]: #util_customizing_util_inspect_colors | ||
| [Internationalization]: intl.html | ||
| [WHATWG Encoding Standard]: https://encoding.spec.whatwg.org/ | ||
| [Common System Errors]: errors.html#common_system_errors | ||
|
||
| [constructor]: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/constructor | ||
| [list of deprecated APIS]: deprecations.html#deprecations_list_of_deprecated_apis | ||
| [semantically incompatible]: https://github.com/nodejs/node/issues/4179 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ const { | |
| arrow_message_private_symbol: kArrowMessagePrivateSymbolIndex, | ||
| decorated_private_symbol: kDecoratedPrivateSymbolIndex | ||
| } = process.binding('util'); | ||
| const { errmap } = process.binding('uv'); | ||
|
|
||
| const noCrypto = !process.versions.openssl; | ||
|
|
||
|
|
@@ -213,6 +214,14 @@ function getConstructorOf(obj) { | |
| return null; | ||
| } | ||
|
|
||
| function getErrorName(code) { | ||
| if (errmap.has(code)) { | ||
|
||
| return errmap.get(code)[0]; | ||
| } else { | ||
| return `Unknown system error ${code}`; | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than the separate function getErrorName(code) {
const entry = errmap.get(code);
return entry ? entry[0] : `Unknown system error ${code}`;
} |
||
| } | ||
|
|
||
| // getConstructorOf is wrapped into this to save iterations | ||
| function getIdentificationOf(obj) { | ||
| const original = obj; | ||
|
|
@@ -340,6 +349,7 @@ module.exports = { | |
| emitExperimentalWarning, | ||
| filterDuplicateStrings, | ||
| getConstructorOf, | ||
| getErrorName, | ||
| getIdentificationOf, | ||
| isError, | ||
| join, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
console.error(name)?