Skip to content

Commit 67b5985

Browse files
committed
net: fix usage of writeBuffer in makeSyncWrite
The binding writeBuffer has been changed in #19041 and it now requires the last argument to be a context object. makeSyncWrite was not updated accordingly, resulting assertions on Windows. This patch fixes the usage of writeBuffer there. Also fix errors.uvException() so error.message are no longer enumerable, this fixes the deepStrictEqual assertion on the error object in test-stdout-close-catch. PR-URL: #19103 Refs: #19041 Reviewed-By: Anna Henningsen <[email protected]>
1 parent 4bfc03b commit 67b5985

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

lib/internal/errors.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,23 @@ function message(key, args) {
426426
* @returns {Error}
427427
*/
428428
function uvException(ctx) {
429-
const err = new Error();
429+
const [ code, uvmsg ] = errmap.get(ctx.errno);
430+
let message = `${code}: ${uvmsg}, ${ctx.syscall}`;
431+
432+
let path;
433+
let dest;
434+
if (ctx.path) {
435+
path = ctx.path.toString();
436+
message += ` '${path}'`;
437+
}
438+
if (ctx.dest) {
439+
dest = ctx.dest.toString();
440+
message += ` -> '${dest}'`;
441+
}
442+
443+
// Pass the message to the constructor instead of setting it on the object
444+
// to make sure it is the same as the one created in C++
445+
const err = new Error(message);
430446

431447
for (const prop of Object.keys(ctx)) {
432448
if (prop === 'message' || prop === 'path' || prop === 'dest') {
@@ -435,20 +451,13 @@ function uvException(ctx) {
435451
err[prop] = ctx[prop];
436452
}
437453

438-
const [ code, uvmsg ] = errmap.get(ctx.errno);
439454
err.code = code;
440-
let message = `${code}: ${uvmsg}, ${ctx.syscall}`;
441-
if (ctx.path) {
442-
const path = ctx.path.toString();
443-
message += ` '${path}'`;
455+
if (path) {
444456
err.path = path;
445457
}
446-
if (ctx.dest) {
447-
const dest = ctx.dest.toString();
448-
message += ` -> '${dest}'`;
458+
if (dest) {
449459
err.dest = dest;
450460
}
451-
err.message = message;
452461

453462
Error.captureStackTrace(err, uvException);
454463
return err;

lib/internal/net.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const Buffer = require('buffer').Buffer;
44
const { isIPv6 } = process.binding('cares_wrap');
55
const { writeBuffer } = process.binding('fs');
6+
const errors = require('internal/errors');
67

78
const octet = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
89
const re = new RegExp(`^${octet}[.]${octet}[.]${octet}[.]${octet}$`);
@@ -33,13 +34,13 @@ function makeSyncWrite(fd) {
3334

3435
this._bytesDispatched += chunk.length;
3536

36-
try {
37-
writeBuffer(fd, chunk, 0, chunk.length, null);
38-
} catch (ex) {
37+
const ctx = {};
38+
writeBuffer(fd, chunk, 0, chunk.length, null, undefined, ctx);
39+
if (ctx.errno !== undefined) {
40+
const ex = errors.uvException(ctx);
3941
// Legacy: net writes have .code === .errno, whereas writeBuffer gives the
4042
// raw errno number in .errno.
41-
if (typeof ex.code === 'string')
42-
ex.errno = ex.code;
43+
ex.errno = ex.code;
4344
return cb(ex);
4445
}
4546
cb();

0 commit comments

Comments
 (0)