GeezerVison is showing github.com — util: add getSystemErrorMap() impl · nodejs/node@08a6d9e
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/util.md +19Lines changed: 19 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -376,6 +376,25 @@ fs.access('file/that/does/not/exist', (err) => { | 376 | 376 | }); | 377 | 377 | ``` | 378 | 378 |
| | 379 | +## `util.getSystemErrorMap()` | | 380 | +<!-- YAML | | 381 | +added: REPLACEME | | 382 | +--> | | 383 | + | | 384 | +* Returns: {Map} | | 385 | + | | 386 | +Returns a Map of all system error codes available from the Node.js API. | | 387 | +The mapping between error codes and error names is platform-dependent. | | 388 | +See [Common System Errors][] for the names of common errors. | | 389 | + | | 390 | +```js | | 391 | +fs.access('file/that/does/not/exist', (err) => { | | 392 | + const errorMap = util.getSystemErrorMap(); | | 393 | + const name = errorMap.get(err.errno); | | 394 | + console.error(name); // ENOENT | | 395 | +}); | | 396 | +``` | | 397 | + | 379 | 398 | ## `util.inherits(constructor, superConstructor)` | 380 | 399 | <!-- YAML | 381 | 400 | added: v0.3.0 | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/util.js +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -44,6 +44,13 @@ const experimentalWarnings = new Set(); | 44 | 44 |
| 45 | 45 | const colorRegExp = /\u001b\[\d\d?m/g; // eslint-disable-line no-control-regex | 46 | 46 |
| | 47 | +let uvBinding; | | 48 | + | | 49 | +function lazyUv() { | | 50 | + uvBinding = uvBinding ?? internalBinding('uv'); | | 51 | + return uvBinding; | | 52 | +} | | 53 | + | 47 | 54 | function removeColors(str) { | 48 | 55 | return str.replace(colorRegExp, ''); | 49 | 56 | } | @@ -271,6 +278,10 @@ function getSystemErrorName(err) { | 271 | 278 | return entry ? entry[0] : `Unknown system error ${err}`; | 272 | 279 | } | 273 | 280 |
| | 281 | +function getSystemErrorMap() { | | 282 | + return lazyUv().getErrorMap(); | | 283 | +} | | 284 | + | 274 | 285 | const kCustomPromisifiedSymbol = SymbolFor('nodejs.util.promisify.custom'); | 275 | 286 | const kCustomPromisifyArgsSymbol = Symbol('customPromisifyArgs'); | 276 | 287 |
| @@ -414,6 +425,7 @@ module.exports = { | 414 | 425 | emitExperimentalWarning, | 415 | 426 | filterDuplicateStrings, | 416 | 427 | getConstructorOf, | | 428 | + getSystemErrorMap, | 417 | 429 | getSystemErrorName, | 418 | 430 | isError, | 419 | 431 | isInsideNodeModules, | |
Collapse file Copy file name to clipboardExpand all lines: lib/util.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -57,6 +57,7 @@ const types = require('internal/util/types'); | 57 | 57 |
| 58 | 58 | const { | 59 | 59 | deprecate, | | 60 | + getSystemErrorMap, | 60 | 61 | getSystemErrorName: internalErrorName, | 61 | 62 | promisify | 62 | 63 | } = require('internal/util'); | @@ -244,6 +245,7 @@ module.exports = { | 244 | 245 | deprecate, | 245 | 246 | format, | 246 | 247 | formatWithOptions, | | 248 | + getSystemErrorMap, | 247 | 249 | getSystemErrorName, | 248 | 250 | inherits, | 249 | 251 | inspect, | |
Collapse file Copy file name to clipboard +24Lines changed: 24 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// Flags: --expose-internals | | 2 | +'use strict'; | | 3 | + | | 4 | +require('../common'); | | 5 | +const assert = require('assert'); | | 6 | +const { | | 7 | + getSystemErrorMap, | | 8 | + _errnoException | | 9 | +} = require('util'); | | 10 | + | | 11 | +const { internalBinding } = require('internal/test/binding'); | | 12 | +const uv = internalBinding('uv'); | | 13 | +const uvKeys = Object.keys(uv); | | 14 | + | | 15 | +const errMap = getSystemErrorMap(); | | 16 | + | | 17 | +uvKeys.forEach((key) => { | | 18 | + if (!key.startsWith('UV_')) | | 19 | + return; | | 20 | + | | 21 | + const err = _errnoException(uv[key]); | | 22 | + const name = uv.errname(uv[key]); | | 23 | + assert.strictEqual(errMap.get(err.errno)[0], name); | | 24 | +}); |
|