Skip to content

Commit

Permalink
fs,cluster,net: assign error codes to remaining errors
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
targos committed Mar 21, 2018
1 parent fddcd62 commit ab8bf26
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 19 deletions.
6 changes: 6 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,12 @@ Encoding provided to `util.TextDecoder()` API was not one of the
A `Promise` that was callbackified via `util.callbackify()` was rejected with a
falsy value.

<a id="ERR_FS_FILE_TOO_LARGE"></a>
### ERR_FS_FILE_TOO_LARGE

An attempt has been made to read a file whose size is larger than the maximum
allowed size for a `Buffer`.

<a id="ERR_FS_INVALID_SYMLINK_TYPE"></a>
### ERR_FS_INVALID_SYMLINK_TYPE

Expand Down
7 changes: 5 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const fs = exports;
const { Buffer } = require('buffer');
const errors = require('internal/errors');
const {
ERR_FS_FILE_TOO_LARGE,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_CALLBACK,
ERR_OUT_OF_RANGE
Expand Down Expand Up @@ -347,8 +348,7 @@ function readFileAfterStat(err) {
}

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

Expand Down Expand Up @@ -421,6 +421,9 @@ function tryCreateBuffer(size, fd, isUserFd) {
var threw = true;
var buffer;
try {
if (size > kMaxLength) {
throw new ERR_FS_FILE_TOO_LARGE(size);
}
buffer = Buffer.allocUnsafe(size);
threw = false;
} finally {
Expand Down
4 changes: 2 additions & 2 deletions lib/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const {
const binding = process.binding('fs');
const { Buffer, kMaxLength } = require('buffer');
const {
ERR_BUFFER_TOO_LARGE,
ERR_FS_FILE_TOO_LARGE,
ERR_INVALID_ARG_TYPE,
ERR_METHOD_NOT_IMPLEMENTED,
ERR_OUT_OF_RANGE
Expand Down Expand Up @@ -143,7 +143,7 @@ async function readFileHandle(filehandle, options) {
return Buffer.alloc(0);

if (size > kMaxLength)
throw new ERR_BUFFER_TOO_LARGE();
throw new ERR_FS_FILE_TOO_LARGE(size);

const chunks = [];
const chunkSize = Math.min(size, 16384);
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/cluster/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const RoundRobinHandle = require('internal/cluster/round_robin_handle');
const SharedHandle = require('internal/cluster/shared_handle');
const Worker = require('internal/cluster/worker');
const { internal, sendHelper, handles } = require('internal/cluster/utils');
const { ERR_SOCKET_BAD_PORT } = require('internal/errors').codes;
const keys = Object.keys;
const cluster = new EventEmitter();
const intercom = new EventEmitter();
Expand Down Expand Up @@ -115,8 +116,7 @@ function createWorkerProcess(id, env) {
inspectPort = cluster.settings.inspectPort;

if (!isLegalPort(inspectPort)) {
throw new TypeError('cluster.settings.inspectPort' +
' is invalid');
throw new ERR_SOCKET_BAD_PORT(inspectPort);
}
} else {
inspectPort = process.debugPort + debugPortOffset;
Expand Down
3 changes: 3 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,9 @@ E('ERR_ENCODING_INVALID_ENCODED_DATA',
E('ERR_ENCODING_NOT_SUPPORTED', 'The "%s" encoding is not supported',
RangeError);
E('ERR_FALSY_VALUE_REJECTION', 'Promise was rejected with falsy value', Error);
E('ERR_FS_FILE_TOO_LARGE', 'File size (%s) is greater than possible Buffer: ' +
`${kMaxLength} bytes`,
RangeError);
E('ERR_FS_INVALID_SYMLINK_TYPE',
'Symlink type must be one of "dir", "file", or "junction". Received "%s"',
Error); // Switch to TypeError. The current implementation does not seem right
Expand Down
2 changes: 1 addition & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ function internalConnect(
localAddress = localAddress || '::';
err = self._handle.bind6(localAddress, localPort);
} else {
self.destroy(new TypeError('Invalid addressType: ' + addressType));
self.destroy(new ERR_INVALID_ADDRESS_FAMILY(addressType));
return;
}
debug('binding to localAddress: %s and localPort: %d (addressType: %d)',
Expand Down
25 changes: 13 additions & 12 deletions test/sequential/test-inspector-port-cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ function testRunnerMain() {
function masterProcessMain() {
const workers = JSON.parse(process.env.workers);
const clusterSettings = JSON.parse(process.env.clusterSettings);
const badPortError = { type: RangeError, code: 'ERR_SOCKET_BAD_PORT' };
let debugPort = process.debugPort;

for (const worker of workers) {
Expand Down Expand Up @@ -234,36 +235,36 @@ function masterProcessMain() {
clusterSettings.inspectPort = 'string';
cluster.setupMaster(clusterSettings);

assert.throws(() => {
common.expectsError(() => {
cluster.fork(params).on('exit', common.mustCall(checkExitCode));
}, TypeError);
}, badPortError);

return;
} else if (clusterSettings.inspectPort === 'null') {
clusterSettings.inspectPort = null;
cluster.setupMaster(clusterSettings);

assert.throws(() => {
common.expectsError(() => {
cluster.fork(params).on('exit', common.mustCall(checkExitCode));
}, TypeError);
}, badPortError);

return;
} else if (clusterSettings.inspectPort === 'bignumber') {
clusterSettings.inspectPort = 1293812;
cluster.setupMaster(clusterSettings);

assert.throws(() => {
common.expectsError(() => {
cluster.fork(params).on('exit', common.mustCall(checkExitCode));
}, TypeError);
}, badPortError);

return;
} else if (clusterSettings.inspectPort === 'negativenumber') {
clusterSettings.inspectPort = -9776;
cluster.setupMaster(clusterSettings);

assert.throws(() => {
common.expectsError(() => {
cluster.fork(params).on('exit', common.mustCall(checkExitCode));
}, TypeError);
}, badPortError);

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

cluster.setupMaster(clusterSettings);

assert.throws(() => {
common.expectsError(() => {
cluster.fork(params).on('exit', common.mustCall(checkExitCode));
}, TypeError);
}, badPortError);

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

cluster.setupMaster(clusterSettings);

assert.throws(() => {
common.expectsError(() => {
cluster.fork(params).on('exit', common.mustCall(checkExitCode));
}, TypeError);
}, badPortError);

return;
}
Expand Down

0 comments on commit ab8bf26

Please sign in to comment.