File tree 4 files changed +57
-0
lines changed
4 files changed +57
-0
lines changed 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
Original file line number Diff line number Diff line change @@ -53,6 +53,13 @@ const experimentalWarnings = new SafeSet();
53
53
54
54
const colorRegExp = / \u001b \[ \d \d ? m / g; // eslint-disable-line no-control-regex
55
55
56
+ let uvBinding ;
57
+
58
+ function lazyUv ( ) {
59
+ uvBinding ??= internalBinding ( 'uv' ) ;
60
+ return uvBinding ;
61
+ }
62
+
56
63
function removeColors ( str ) {
57
64
return StringPrototypeReplace ( str , colorRegExp , '' ) ;
58
65
}
@@ -286,6 +293,10 @@ function getSystemErrorName(err) {
286
293
return entry ? entry [ 0 ] : `Unknown system error ${ err } ` ;
287
294
}
288
295
296
+ function getSystemErrorMap ( ) {
297
+ return lazyUv ( ) . getErrorMap ( ) ;
298
+ }
299
+
289
300
const kCustomPromisifiedSymbol = SymbolFor ( 'nodejs.util.promisify.custom' ) ;
290
301
const kCustomPromisifyArgsSymbol = Symbol ( 'customPromisifyArgs' ) ;
291
302
@@ -442,6 +453,7 @@ module.exports = {
442
453
emitExperimentalWarning,
443
454
filterDuplicateStrings,
444
455
getConstructorOf,
456
+ getSystemErrorMap,
445
457
getSystemErrorName,
446
458
isError,
447
459
isInsideNodeModules,
Original file line number Diff line number Diff line change @@ -67,6 +67,7 @@ const types = require('internal/util/types');
67
67
68
68
const {
69
69
deprecate,
70
+ getSystemErrorMap,
70
71
getSystemErrorName : internalErrorName ,
71
72
promisify
72
73
} = require ( 'internal/util' ) ;
@@ -256,6 +257,7 @@ module.exports = {
256
257
deprecate,
257
258
format,
258
259
formatWithOptions,
260
+ getSystemErrorMap,
259
261
getSystemErrorName,
260
262
inherits,
261
263
inspect,
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments