Skip to content

Commit 8021710

Browse files
eladkishonjasnell
authored andcommitted
util: add getSystemErrorMap() impl
PR-URL: #38101Fixes: #37951 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 4def7c4 commit 8021710

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

‎doc/api/util.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,25 @@ fs.access('file/that/does/not/exist', (err) =>{
376376
});
377377
```
378378

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+
consterrorMap=util.getSystemErrorMap();
393+
constname=errorMap.get(err.errno);
394+
console.error(name); // ENOENT
395+
});
396+
```
397+
379398
## `util.inherits(constructor, superConstructor)`
380399
<!-- YAML
381400
added: v0.3.0

‎lib/internal/util.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ const experimentalWarnings = new SafeSet();
5353

5454
constcolorRegExp=/\u001b\[\d\d?m/g;// eslint-disable-line no-control-regex
5555

56+
letuvBinding;
57+
58+
functionlazyUv(){
59+
uvBinding??=internalBinding('uv');
60+
returnuvBinding;
61+
}
62+
5663
functionremoveColors(str){
5764
returnStringPrototypeReplace(str,colorRegExp,'');
5865
}
@@ -286,6 +293,10 @@ function getSystemErrorName(err){
286293
returnentry ? entry[0] : `Unknown system error ${err}`;
287294
}
288295

296+
functiongetSystemErrorMap(){
297+
returnlazyUv().getErrorMap();
298+
}
299+
289300
constkCustomPromisifiedSymbol=SymbolFor('nodejs.util.promisify.custom');
290301
constkCustomPromisifyArgsSymbol=Symbol('customPromisifyArgs');
291302

@@ -442,6 +453,7 @@ module.exports ={
442453
emitExperimentalWarning,
443454
filterDuplicateStrings,
444455
getConstructorOf,
456+
getSystemErrorMap,
445457
getSystemErrorName,
446458
isError,
447459
isInsideNodeModules,

‎lib/util.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const types = require('internal/util/types');
6767

6868
const{
6969
deprecate,
70+
getSystemErrorMap,
7071
getSystemErrorName: internalErrorName,
7172
promisify
7273
}=require('internal/util');
@@ -256,6 +257,7 @@ module.exports ={
256257
deprecate,
257258
format,
258259
formatWithOptions,
260+
getSystemErrorMap,
259261
getSystemErrorName,
260262
inherits,
261263
inspect,

‎test/parallel/test-uv-errmap.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
4+
require('../common');
5+
constassert=require('assert');
6+
const{
7+
getSystemErrorMap,
8+
_errnoException
9+
}=require('util');
10+
11+
const{ internalBinding }=require('internal/test/binding');
12+
constuv=internalBinding('uv');
13+
constuvKeys=Object.keys(uv);
14+
15+
consterrMap=getSystemErrorMap();
16+
17+
uvKeys.forEach((key)=>{
18+
if(!key.startsWith('UV_'))
19+
return;
20+
21+
consterr=_errnoException(uv[key]);
22+
constname=uv.errname(uv[key]);
23+
assert.strictEqual(errMap.get(err.errno)[0],name);
24+
});

0 commit comments

Comments
(0)