|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +require('../common'); |
| 4 | +const assert = require('node:assert'); |
| 5 | +const os = require('node:os'); |
| 6 | +const util = require('node:util'); |
| 7 | + |
| 8 | +const ENOENT = -os.constants.errno.ENOENT; |
| 9 | +const EACCES = -os.constants.errno.EACCES; |
| 10 | + |
| 11 | +// Address and port are included in the message |
| 12 | +{ |
| 13 | + const e = util._exceptionWithHostPort(ENOENT, 'connect', '127.0.0.1', 8080); |
| 14 | + assert.ok(e instanceof Error); |
| 15 | + assert.strictEqual(e.errno, ENOENT); |
| 16 | + assert.strictEqual(e.code, util.getSystemErrorName(ENOENT)); |
| 17 | + assert.strictEqual(e.syscall, 'connect'); |
| 18 | + assert.strictEqual(e.address, '127.0.0.1'); |
| 19 | + assert.strictEqual(e.port, 8080); |
| 20 | + assert.ok(e.message.includes('127.0.0.1:8080'), `expected 127.0.0.1:8080 in "${e.message}"`); |
| 21 | +} |
| 22 | + |
| 23 | +// Port=0 omits host:port detail; port property is not set |
| 24 | +{ |
| 25 | + const e = util._exceptionWithHostPort(ENOENT, 'connect', '127.0.0.1', 0); |
| 26 | + assert.strictEqual(e.address, '127.0.0.1'); |
| 27 | + assert.strictEqual(e.port, undefined); |
| 28 | + assert.ok(e.message.includes('127.0.0.1'), `expected address in "${e.message}"`); |
| 29 | + assert.ok(!e.message.includes(':0'), `unexpected :0 in "${e.message}"`); |
| 30 | +} |
| 31 | + |
| 32 | +// Additional is appended as "- Local (...)" |
| 33 | +{ |
| 34 | + const e = util._exceptionWithHostPort(ENOENT, 'connect', '127.0.0.1', 80, '0.0.0.0:12345'); |
| 35 | + assert.ok(e.message.includes('- Local (0.0.0.0:12345)'), `expected local info in "${e.message}"`); |
| 36 | +} |
| 37 | + |
| 38 | +// No address or port |
| 39 | +{ |
| 40 | + const e = util._exceptionWithHostPort(ENOENT, 'connect', null, 0); |
| 41 | + assert.strictEqual(e.address, null); |
| 42 | + assert.strictEqual(e.port, undefined); |
| 43 | + assert.ok(!e.message.includes('null'), `unexpected null in "${e.message}"`); |
| 44 | +} |
| 45 | + |
| 46 | +// Different syscall and error code |
| 47 | +{ |
| 48 | + const e = util._exceptionWithHostPort(EACCES, 'bind', '0.0.0.0', 443); |
| 49 | + assert.strictEqual(e.code, util.getSystemErrorName(EACCES)); |
| 50 | + assert.strictEqual(e.syscall, 'bind'); |
| 51 | +} |
| 52 | + |
| 53 | +// Stack trace should not include _exceptionWithHostPort itself |
| 54 | +{ |
| 55 | + const e = util._exceptionWithHostPort(ENOENT, 'connect', '127.0.0.1', 80); |
| 56 | + assert.ok(!e.stack.includes('_exceptionWithHostPort'), 'stack must not mention the wrapper'); |
| 57 | +} |
0 commit comments