Skip to content

Commit ab8bf26

Browse files
committed
fs,cluster,net: assign error codes to remaining errors
After this commit, all errors thrown from JS code in lib have an error code. PR-URL: #19373 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent fddcd62 commit ab8bf26

File tree

7 files changed

+32
-19
lines changed

7 files changed

+32
-19
lines changed

doc/api/errors.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,12 @@ Encoding provided to `util.TextDecoder()` API was not one of the
777777
A `Promise` that was callbackified via `util.callbackify()` was rejected with a
778778
falsy value.
779779

780+
<a id="ERR_FS_FILE_TOO_LARGE"></a>
781+
### ERR_FS_FILE_TOO_LARGE
782+
783+
An attempt has been made to read a file whose size is larger than the maximum
784+
allowed size for a `Buffer`.
785+
780786
<a id="ERR_FS_INVALID_SYMLINK_TYPE"></a>
781787
### ERR_FS_INVALID_SYMLINK_TYPE
782788

lib/fs.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const fs = exports;
3636
const { Buffer } = require('buffer');
3737
const errors = require('internal/errors');
3838
const {
39+
ERR_FS_FILE_TOO_LARGE,
3940
ERR_INVALID_ARG_TYPE,
4041
ERR_INVALID_CALLBACK,
4142
ERR_OUT_OF_RANGE
@@ -347,8 +348,7 @@ function readFileAfterStat(err) {
347348
}
348349

349350
if (size > kMaxLength) {
350-
err = new RangeError('File size is greater than possible Buffer: ' +
351-
`0x${kMaxLength.toString(16)} bytes`);
351+
err = new ERR_FS_FILE_TOO_LARGE(size);
352352
return context.close(err);
353353
}
354354

@@ -421,6 +421,9 @@ function tryCreateBuffer(size, fd, isUserFd) {
421421
var threw = true;
422422
var buffer;
423423
try {
424+
if (size > kMaxLength) {
425+
throw new ERR_FS_FILE_TOO_LARGE(size);
426+
}
424427
buffer = Buffer.allocUnsafe(size);
425428
threw = false;
426429
} finally {

lib/fs/promises.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const {
1313
const binding = process.binding('fs');
1414
const { Buffer, kMaxLength } = require('buffer');
1515
const {
16-
ERR_BUFFER_TOO_LARGE,
16+
ERR_FS_FILE_TOO_LARGE,
1717
ERR_INVALID_ARG_TYPE,
1818
ERR_METHOD_NOT_IMPLEMENTED,
1919
ERR_OUT_OF_RANGE
@@ -143,7 +143,7 @@ async function readFileHandle(filehandle, options) {
143143
return Buffer.alloc(0);
144144

145145
if (size > kMaxLength)
146-
throw new ERR_BUFFER_TOO_LARGE();
146+
throw new ERR_FS_FILE_TOO_LARGE(size);
147147

148148
const chunks = [];
149149
const chunkSize = Math.min(size, 16384);

lib/internal/cluster/master.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const RoundRobinHandle = require('internal/cluster/round_robin_handle');
88
const SharedHandle = require('internal/cluster/shared_handle');
99
const Worker = require('internal/cluster/worker');
1010
const { internal, sendHelper, handles } = require('internal/cluster/utils');
11+
const { ERR_SOCKET_BAD_PORT } = require('internal/errors').codes;
1112
const keys = Object.keys;
1213
const cluster = new EventEmitter();
1314
const intercom = new EventEmitter();
@@ -115,8 +116,7 @@ function createWorkerProcess(id, env) {
115116
inspectPort = cluster.settings.inspectPort;
116117

117118
if (!isLegalPort(inspectPort)) {
118-
throw new TypeError('cluster.settings.inspectPort' +
119-
' is invalid');
119+
throw new ERR_SOCKET_BAD_PORT(inspectPort);
120120
}
121121
} else {
122122
inspectPort = process.debugPort + debugPortOffset;

lib/internal/errors.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,9 @@ E('ERR_ENCODING_INVALID_ENCODED_DATA',
651651
E('ERR_ENCODING_NOT_SUPPORTED', 'The "%s" encoding is not supported',
652652
RangeError);
653653
E('ERR_FALSY_VALUE_REJECTION', 'Promise was rejected with falsy value', Error);
654+
E('ERR_FS_FILE_TOO_LARGE', 'File size (%s) is greater than possible Buffer: ' +
655+
`${kMaxLength} bytes`,
656+
RangeError);
654657
E('ERR_FS_INVALID_SYMLINK_TYPE',
655658
'Symlink type must be one of "dir", "file", or "junction". Received "%s"',
656659
Error); // Switch to TypeError. The current implementation does not seem right

lib/net.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ function internalConnect(
928928
localAddress = localAddress || '::';
929929
err = self._handle.bind6(localAddress, localPort);
930930
} else {
931-
self.destroy(new TypeError('Invalid addressType: ' + addressType));
931+
self.destroy(new ERR_INVALID_ADDRESS_FAMILY(addressType));
932932
return;
933933
}
934934
debug('binding to localAddress: %s and localPort: %d (addressType: %d)',

test/sequential/test-inspector-port-cluster.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ function testRunnerMain() {
207207
function masterProcessMain() {
208208
const workers = JSON.parse(process.env.workers);
209209
const clusterSettings = JSON.parse(process.env.clusterSettings);
210+
const badPortError = { type: RangeError, code: 'ERR_SOCKET_BAD_PORT' };
210211
let debugPort = process.debugPort;
211212

212213
for (const worker of workers) {
@@ -234,36 +235,36 @@ function masterProcessMain() {
234235
clusterSettings.inspectPort = 'string';
235236
cluster.setupMaster(clusterSettings);
236237

237-
assert.throws(() => {
238+
common.expectsError(() => {
238239
cluster.fork(params).on('exit', common.mustCall(checkExitCode));
239-
}, TypeError);
240+
}, badPortError);
240241

241242
return;
242243
} else if (clusterSettings.inspectPort === 'null') {
243244
clusterSettings.inspectPort = null;
244245
cluster.setupMaster(clusterSettings);
245246

246-
assert.throws(() => {
247+
common.expectsError(() => {
247248
cluster.fork(params).on('exit', common.mustCall(checkExitCode));
248-
}, TypeError);
249+
}, badPortError);
249250

250251
return;
251252
} else if (clusterSettings.inspectPort === 'bignumber') {
252253
clusterSettings.inspectPort = 1293812;
253254
cluster.setupMaster(clusterSettings);
254255

255-
assert.throws(() => {
256+
common.expectsError(() => {
256257
cluster.fork(params).on('exit', common.mustCall(checkExitCode));
257-
}, TypeError);
258+
}, badPortError);
258259

259260
return;
260261
} else if (clusterSettings.inspectPort === 'negativenumber') {
261262
clusterSettings.inspectPort = -9776;
262263
cluster.setupMaster(clusterSettings);
263264

264-
assert.throws(() => {
265+
common.expectsError(() => {
265266
cluster.fork(params).on('exit', common.mustCall(checkExitCode));
266-
}, TypeError);
267+
}, badPortError);
267268

268269
return;
269270
} else if (clusterSettings.inspectPort === 'bignumberfunc') {
@@ -274,9 +275,9 @@ function masterProcessMain() {
274275

275276
cluster.setupMaster(clusterSettings);
276277

277-
assert.throws(() => {
278+
common.expectsError(() => {
278279
cluster.fork(params).on('exit', common.mustCall(checkExitCode));
279-
}, TypeError);
280+
}, badPortError);
280281

281282
return;
282283
} else if (clusterSettings.inspectPort === 'strfunc') {
@@ -287,9 +288,9 @@ function masterProcessMain() {
287288

288289
cluster.setupMaster(clusterSettings);
289290

290-
assert.throws(() => {
291+
common.expectsError(() => {
291292
cluster.fork(params).on('exit', common.mustCall(checkExitCode));
292-
}, TypeError);
293+
}, badPortError);
293294

294295
return;
295296
}

0 commit comments

Comments
 (0)