GeezerVison is showing github.com — util: implement util.getSystemErrorName() · nodejs/node@28fa906
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 +21-1Lines changed: 21 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -224,6 +224,25 @@ without any formatting. | 224 | 224 | util.format('%% %s'); // '%% %s' | 225 | 225 | ``` | 226 | 226 |
| | 227 | +## util.getSystemErrorName(err) | | 228 | +<!-- YAML | | 229 | +added: REPLACEME | | 230 | +--> | | 231 | + | | 232 | +* `err` {number} | | 233 | +* Returns: {string} | | 234 | + | | 235 | +Returns the string name for a numeric error code that comes from a Node.js API. | | 236 | +The mapping between error codes and error names is platform-dependent. | | 237 | +See [Common System Errors][] for the names of common errors. | | 238 | + | | 239 | +```js | | 240 | +fs.access('file/that/does/not/exist', (err) => { | | 241 | + const name = util.getSystemErrorName(err.errno); | | 242 | + console.error(name); // ENOENT | | 243 | +}); | | 244 | +``` | | 245 | + | 227 | 246 | ## util.inherits(constructor, superConstructor) | 228 | 247 | <!-- YAML | 229 | 248 | added: v0.3.0 | @@ -1222,5 +1241,6 @@ Deprecated predecessor of `console.log`. | 1222 | 1241 | [Customizing `util.inspect` colors]: #util_customizing_util_inspect_colors | 1223 | 1242 | [Internationalization]: intl.html | 1224 | 1243 | [WHATWG Encoding Standard]: https://encoding.spec.whatwg.org/ | 1225 | | -[constructor]: https://developer.mozilla.org/en-US/JavaScript/Reference/Global_Objects/Object/constructor | | 1244 | +[Common System Errors]: errors.html#errors_common_system_errors | | 1245 | +[constructor]: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/constructor | 1226 | 1246 | [semantically incompatible]: https://github.com/nodejs/node/issues/4179 |
Collapse file Copy file name to clipboardExpand all lines: lib/child_process.js +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | 'use strict'; | 23 | 23 |
| 24 | 24 | const util = require('util'); | 25 | | -const { deprecate, convertToValidSignal } = require('internal/util'); | | 25 | +const { | | 26 | + deprecate, convertToValidSignal, getSystemErrorName | | 27 | +} = require('internal/util'); | 26 | 28 | const { isUint8Array } = require('internal/util/types'); | 27 | 29 | const { createPromise, | 28 | 30 | promiseResolve, promiseReject } = process.binding('util'); | 29 | 31 | const debug = util.debuglog('child_process'); | 30 | 32 | const { Buffer } = require('buffer'); | 31 | 33 | const { Pipe, constants: PipeConstants } = process.binding('pipe_wrap'); | 32 | | -const { errname } = process.binding('uv'); | 33 | 34 | const child_process = require('internal/child_process'); | 34 | 35 | const { | 35 | 36 | _validateStdio, | @@ -271,7 +272,7 @@ exports.execFile = function(file /*, args, options, callback*/) { | 271 | 272 | if (!ex) { | 272 | 273 | ex = new Error('Command failed: ' + cmd + '\n' + stderr); | 273 | 274 | ex.killed = child.killed || killed; | 274 | | - ex.code = code < 0 ? errname(code) : code; | | 275 | + ex.code = code < 0 ? getSystemErrorName(code) : code; | 275 | 276 | ex.signal = signal; | 276 | 277 | } | 277 | 278 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/util.js +12-1Lines changed: 12 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | const errors = require('internal/errors'); | 4 | 4 | const binding = process.binding('util'); | 5 | 5 | const { signals } = process.binding('constants').os; | 6 | | - | 7 | 6 | const { createPromise, promiseResolve, promiseReject } = binding; | | 7 | +const { errmap } = process.binding('uv'); | 8 | 8 |
| 9 | 9 | const kArrowMessagePrivateSymbolIndex = binding.arrow_message_private_symbol; | 10 | 10 | const kDecoratedPrivateSymbolIndex = binding.decorated_private_symbol; | @@ -207,6 +207,16 @@ function getConstructorOf(obj) { | 207 | 207 | return null; | 208 | 208 | } | 209 | 209 |
| | 210 | +function getSystemErrorName(err) { | | 211 | + if (typeof err !== 'number' || err >= 0 || !Number.isSafeInteger(err)) { | | 212 | + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'err', | | 213 | + 'negative number'); | | 214 | + } | | 215 | + | | 216 | + const entry = errmap.get(err); | | 217 | + return entry ? entry[0] : `Unknown system error ${err}`; | | 218 | +} | | 219 | + | 210 | 220 | const kCustomPromisifiedSymbol = Symbol('util.promisify.custom'); | 211 | 221 | const kCustomPromisifyArgsSymbol = Symbol('customPromisifyArgs'); | 212 | 222 |
| @@ -297,6 +307,7 @@ module.exports = { | 297 | 307 | emitExperimentalWarning, | 298 | 308 | filterDuplicateStrings, | 299 | 309 | getConstructorOf, | | 310 | + getSystemErrorName, | 300 | 311 | isError, | 301 | 312 | join, | 302 | 313 | normalizeEncoding, | |
Collapse file Copy file name to clipboardExpand all lines: lib/util.js +3-7Lines changed: 3 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,8 +25,6 @@ const errors = require('internal/errors'); | 25 | 25 | const { TextDecoder, TextEncoder } = require('internal/encoding'); | 26 | 26 | const { isBuffer } = require('buffer').Buffer; | 27 | 27 |
| 28 | | -const { errname } = process.binding('uv'); | 29 | | - | 30 | 28 | const { | 31 | 29 | getPromiseDetails, | 32 | 30 | getProxyDetails, | @@ -56,6 +54,7 @@ const { | 56 | 54 | customInspectSymbol, | 57 | 55 | deprecate, | 58 | 56 | getConstructorOf, | | 57 | + getSystemErrorName, | 59 | 58 | isError, | 60 | 59 | promisify, | 61 | 60 | join, | @@ -992,11 +991,7 @@ function error(...args) { | 992 | 991 | } | 993 | 992 |
| 994 | 993 | function _errnoException(err, syscall, original) { | 995 | | - if (typeof err !== 'number' || err >= 0 || !Number.isSafeInteger(err)) { | 996 | | - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'err', | 997 | | - 'negative number'); | 998 | | - } | 999 | | - const name = errname(err); | | 994 | + const name = getSystemErrorName(err); | 1000 | 995 | var message = `${syscall} ${name}`; | 1001 | 996 | if (original) | 1002 | 997 | message += ` ${original}`; | @@ -1085,6 +1080,7 @@ module.exports = exports = { | 1085 | 1080 | debuglog, | 1086 | 1081 | deprecate, | 1087 | 1082 | format, | | 1083 | + getSystemErrorName, | 1088 | 1084 | inherits, | 1089 | 1085 | inspect, | 1090 | 1086 | isArray: Array.isArray, | |
Collapse file Copy file name to clipboardExpand all lines: src/uv.cc +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,6 +39,8 @@ using v8::String; | 39 | 39 | using v8::Value; | 40 | 40 |
| 41 | 41 |
| | 42 | +// TODO(joyeecheung): deprecate this function in favor of | | 43 | +// lib/util.getSystemErrorName() | 42 | 44 | void ErrName(const FunctionCallbackInfo<Value>& args) { | 43 | 45 | Environment* env = Environment::GetCurrent(args); | 44 | 46 | int err = args[0]->Int32Value(); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-child-process-execfile.js +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | | 2 | + | 2 | 3 | const common = require('../common'); | 3 | 4 | const assert = require('assert'); | 4 | 5 | const execFile = require('child_process').execFile; | 5 | | -const uv = process.binding('uv'); | | 6 | +const { getSystemErrorName } = require('util'); | 6 | 7 | const fixtures = require('../common/fixtures'); | 7 | 8 |
| 8 | 9 | const fixture = fixtures.path('exit.js'); | @@ -27,7 +28,7 @@ const execOpts = { encoding: 'utf8', shell: true }; | 27 | 28 | const code = -1; | 28 | 29 | const callback = common.mustCall((err, stdout, stderr) => { | 29 | 30 | assert.strictEqual(err.toString().trim(), errorString); | 30 | | - assert.strictEqual(err.code, uv.errname(code)); | | 31 | + assert.strictEqual(err.code, getSystemErrorName(code)); | 31 | 32 | assert.strictEqual(err.killed, true); | 32 | 33 | assert.strictEqual(err.signal, null); | 33 | 34 | assert.strictEqual(err.cmd, process.execPath); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-net-server-listen-handle.js +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,7 +4,7 @@ const common = require('../common'); | 4 | 4 | const assert = require('assert'); | 5 | 5 | const net = require('net'); | 6 | 6 | const fs = require('fs'); | 7 | | -const uv = process.binding('uv'); | | 7 | +const { getSystemErrorName } = require('util'); | 8 | 8 | const { TCP, constants: TCPConstants } = process.binding('tcp_wrap'); | 9 | 9 | const { Pipe, constants: PipeConstants } = process.binding('pipe_wrap'); | 10 | 10 |
| @@ -47,9 +47,10 @@ function randomHandle(type) { | 47 | 47 | handleName = `pipe ${path}`; | 48 | 48 | } | 49 | 49 |
| 50 | | - if (errno < 0) { // uv.errname requires err < 0 | 51 | | - assert(errno >= 0, `unable to bind ${handleName}: ${uv.errname(errno)}`); | | 50 | + if (errno < 0) { | | 51 | + assert.fail(`unable to bind ${handleName}: ${getSystemErrorName(errno)}`); | 52 | 52 | } | | 53 | + | 53 | 54 | if (!common.isWindows) { // fd doesn't work on windows | 54 | 55 | // err >= 0 but fd = -1, should not happen | 55 | 56 | assert.notStrictEqual(handle.fd, -1, | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-uv-errno.js +9-5Lines changed: 9 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | const common = require('../common'); | 4 | 4 | const assert = require('assert'); | 5 | | -const util = require('util'); | 6 | | -const uv = process.binding('uv'); | | 5 | +const { | | 6 | + getSystemErrorName, | | 7 | + _errnoException | | 8 | +} = require('util'); | 7 | 9 |
| | 10 | +const uv = process.binding('uv'); | 8 | 11 | const keys = Object.keys(uv); | 9 | 12 |
| 10 | 13 | keys.forEach((key) => { | 11 | 14 | if (!key.startsWith('UV_')) | 12 | 15 | return; | 13 | 16 |
| 14 | 17 | assert.doesNotThrow(() => { | 15 | | - const err = util._errnoException(uv[key], 'test'); | | 18 | + const err = _errnoException(uv[key], 'test'); | 16 | 19 | const name = uv.errname(uv[key]); | 17 | | - assert.strictEqual(err.code, err.errno); | | 20 | + assert.strictEqual(getSystemErrorName(uv[key]), name); | 18 | 21 | assert.strictEqual(err.code, name); | | 22 | + assert.strictEqual(err.code, err.errno); | 19 | 23 | assert.strictEqual(err.message, `test ${name}`); | 20 | 24 | }); | 21 | 25 | }); | 22 | 26 |
| 23 | 27 | [0, 1, 'test', {}, [], Infinity, -Infinity, NaN].forEach((key) => { | 24 | 28 | common.expectsError( | 25 | | - () => util._errnoException(key), | | 29 | + () => _errnoException(key), | 26 | 30 | { | 27 | 31 | code: 'ERR_INVALID_ARG_TYPE', | 28 | 32 | type: TypeError, | |
Collapse file Copy file name to clipboardExpand all lines: test/sequential/test-async-wrap-getasyncid.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,6 +7,7 @@ const net = require('net'); | 7 | 7 | const providers = Object.assign({}, process.binding('async_wrap').Providers); | 8 | 8 | const fixtures = require('../common/fixtures'); | 9 | 9 | const tmpdir = require('../common/tmpdir'); | | 10 | +const { getSystemErrorName } = require('util'); | 10 | 11 |
| 11 | 12 | // Make sure that all Providers are tested. | 12 | 13 | { | @@ -214,7 +215,7 @@ if (common.hasCrypto) { // eslint-disable-line crypto-check | 214 | 215 | // Use a long string to make sure the write happens asynchronously. | 215 | 216 | const err = handle.writeLatin1String(wreq, 'hi'.repeat(100000)); | 216 | 217 | if (err) | 217 | | - throw new Error(`write failed: ${process.binding('uv').errname(err)}`); | | 218 | + throw new Error(`write failed: ${getSystemErrorName(err)}`); | 218 | 219 | testInitialized(wreq, 'WriteWrap'); | 219 | 220 | }); | 220 | 221 | req.address = common.localhostIPv4; | |
|