Skip to content

Commit 3663fc8

Browse files
sagitsofanjasnell
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 <anna@addaleax.net> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent f40b1db commit 3663fc8

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

lib/internal/errors.js

+44-2
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,46 @@ function uvException(ctx) {
285285
return err;
286286
}
287287

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

316356
/**
317-
* This used to be util._exceptionWithHostPort().
318-
*
357+
* Deprecated, new function is `uvExceptionWithHostPort()`
358+
* New function added the error description directly
359+
* from C++. this method for backwards compatibility
319360
* @param {number} err - A libuv error number
320361
* @param {string} syscall
321362
* @param {string} address
@@ -437,6 +478,7 @@ module.exports = {
437478
errnoException,
438479
exceptionWithHostPort,
439480
uvException,
481+
uvExceptionWithHostPort,
440482
isStackOverflowError,
441483
getMessage,
442484
SystemError,

lib/net.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ const kLastWriteQueueSize = Symbol('lastWriteQueueSize');
8484
let cluster;
8585
let dns;
8686

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

8993
const {
9094
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ if (!common.isWindows) { // Windows doesn't support {fd: <n>}
150150
net.createServer()
151151
.listen({ fd }, common.mustNotCall())
152152
.on('error', common.mustCall(function(err) {
153-
assert.strictEqual(String(err), 'Error: listen EINVAL');
153+
assert.strictEqual(String(err), 'Error: listen EINVAL: invalid argument');
154154
this.close();
155155
}));
156156
}

0 commit comments

Comments
 (0)