Skip to content

Commit ad59bbd

Browse files
sagitsofanTrott
authored andcommitted
lib: http server, friendly error messages
Improved error message description for the http server binding errors. Currently changed only in `setupListenHandle`, but needs to be change all over. Added new `uvExceptionWithHostPort` function (+export) in `lib/internal/error.js` that extracts the error message defined by libuv, using the error code, and returns an error object with the full error description. example: old error message: `listen EADDRINUSE` new error message: `listen EADDRINUSE: Address already in use` Removed exportable function `_exceptionWithHostPort` from `lib/util.js` - exported by accident Replaced `exceptionWithHostPort` to the new function `uvExceptionWithHostPort` for a more detailed error. Fixes: #22936 PR-URL: #22995 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent f2b4734 commit ad59bbd

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

lib/internal/errors.js

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,46 @@ function uvException(ctx) {
286286
return err;
287287
}
288288

289+
/**
290+
* This creates an error compatible with errors produced in the C++
291+
* This function should replace the deprecated
292+
* `exceptionWithHostPort()` function.
293+
*
294+
* @param {number} err - A libuv error number
295+
* @param {string} syscall
296+
* @param {string} address
297+
* @param {number} [port]
298+
* @param {string} [additional]
299+
* @returns {Error}
300+
*/
301+
function uvExceptionWithHostPort(err, syscall, address, port, additional) {
302+
const [ code, uvmsg ] = errmap.get(err);
303+
const message = `${syscall} ${code}: ${uvmsg}`;
304+
let details = '';
305+
306+
if (port && port > 0) {
307+
details = ` ${address}:${port}`;
308+
} else if (address) {
309+
details = ` ${address}`;
310+
}
311+
if (additional) {
312+
details += ` - Local (${additional})`;
313+
}
314+
315+
// eslint-disable-next-line no-restricted-syntax
316+
const ex = new Error(`${message}${details}`);
317+
ex.code = code;
318+
ex.errno = code;
319+
ex.syscall = syscall;
320+
ex.address = address;
321+
if (port) {
322+
ex.port = port;
323+
}
324+
325+
Error.captureStackTrace(ex, uvExceptionWithHostPort);
326+
return ex;
327+
}
328+
289329
/**
290330
* This used to be util._errnoException().
291331
*
@@ -315,8 +355,9 @@ function errnoException(err, syscall, original) {
315355
}
316356

317357
/**
318-
* This used to be util._exceptionWithHostPort().
319-
*
358+
* Deprecated, new function is `uvExceptionWithHostPort()`
359+
* New function added the error description directly
360+
* from C++. this method for backwards compatibility
320361
* @param {number} err - A libuv error number
321362
* @param {string} syscall
322363
* @param {string} address
@@ -440,6 +481,7 @@ module.exports = {
440481
errnoException,
441482
exceptionWithHostPort,
442483
uvException,
484+
uvExceptionWithHostPort,
443485
isStackOverflowError,
444486
getMessage,
445487
SystemError,

lib/net.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ const kLastWriteQueueSize = Symbol('lastWriteQueueSize');
8383
let cluster;
8484
let dns;
8585

86-
const { errnoException, exceptionWithHostPort } = errors;
86+
const {
87+
errnoException,
88+
exceptionWithHostPort,
89+
uvExceptionWithHostPort
90+
} = errors;
8791

8892
const {
8993
kTimeout,
@@ -1266,7 +1270,7 @@ function setupListenHandle(address, port, addressType, backlog, fd) {
12661270
rval = createServerHandle(address, port, addressType, fd);
12671271

12681272
if (typeof rval === 'number') {
1269-
var error = exceptionWithHostPort(rval, 'listen', address, port);
1273+
var error = uvExceptionWithHostPort(rval, 'listen', address, port);
12701274
process.nextTick(emitErrorNT, this, error);
12711275
return;
12721276
}
@@ -1283,7 +1287,7 @@ function setupListenHandle(address, port, addressType, backlog, fd) {
12831287
var err = this._handle.listen(backlog || 511);
12841288

12851289
if (err) {
1286-
var ex = exceptionWithHostPort(err, 'listen', address, port);
1290+
var ex = uvExceptionWithHostPort(err, 'listen', address, port);
12871291
this._handle.close();
12881292
this._handle = null;
12891293
defaultTriggerAsyncIdScope(this[async_id_symbol],

test/parallel/test-net-server-listen-handle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ if (!common.isWindows) { // Windows doesn't support {fd: <n>}
148148
net.createServer()
149149
.listen({ fd }, common.mustNotCall())
150150
.on('error', common.mustCall(function(err) {
151-
assert.strictEqual(String(err), 'Error: listen EINVAL');
151+
assert.strictEqual(String(err), 'Error: listen EINVAL: invalid argument');
152152
this.close();
153153
}));
154154
}

0 commit comments

Comments
 (0)