aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichaël Zasso <targos@protonmail.com>2018-03-15 14:22:36 +0100
committerMichaël Zasso <targos@protonmail.com>2018-03-21 20:14:57 +0100
commitab8bf26994677a5f0823b3810668f6cfa18374d9 (patch)
tree3f7e5a5f72d49d3fc3c1c0600c16234d15b1bda8
parentfddcd6253b237ca68430f44c7614bf52d57c4f97 (diff)
downloadandroid-node-v8-ab8bf26994677a5f0823b3810668f6cfa18374d9.tar.gz
android-node-v8-ab8bf26994677a5f0823b3810668f6cfa18374d9.tar.bz2
android-node-v8-ab8bf26994677a5f0823b3810668f6cfa18374d9.zip
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: https://github.com/nodejs/node/pull/19373 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
-rw-r--r--doc/api/errors.md6
-rw-r--r--lib/fs.js7
-rw-r--r--lib/fs/promises.js4
-rw-r--r--lib/internal/cluster/master.js4
-rw-r--r--lib/internal/errors.js3
-rw-r--r--lib/net.js2
-rw-r--r--test/sequential/test-inspector-port-cluster.js25
7 files changed, 32 insertions, 19 deletions
diff --git a/doc/api/errors.md b/doc/api/errors.md
index 5e94535a3e..62a62d7665 100644
--- a/doc/api/errors.md
+++ b/doc/api/errors.md
@@ -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
diff --git a/lib/fs.js b/lib/fs.js
index f6722921fc..ec4a1e9a9f 100644
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -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
@@ -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);
}
@@ -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 {
diff --git a/lib/fs/promises.js b/lib/fs/promises.js
index 2252900e22..a36d845d51 100644
--- a/lib/fs/promises.js
+++ b/lib/fs/promises.js
@@ -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
@@ -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);
diff --git a/lib/internal/cluster/master.js b/lib/internal/cluster/master.js
index 3c6a398f11..7ee43689c4 100644
--- a/lib/internal/cluster/master.js
+++ b/lib/internal/cluster/master.js
@@ -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();
@@ -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;
diff --git a/lib/internal/errors.js b/lib/internal/errors.js
index 33a3185f79..f7eeb946b6 100644
--- a/lib/internal/errors.js
+++ b/lib/internal/errors.js
@@ -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
diff --git a/lib/net.js b/lib/net.js
index f97b68d4c4..3693aeb690 100644
--- a/lib/net.js
+++ b/lib/net.js
@@ -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)',
diff --git a/test/sequential/test-inspector-port-cluster.js b/test/sequential/test-inspector-port-cluster.js
index 87469aa7ff..dfaf59c711 100644
--- a/test/sequential/test-inspector-port-cluster.js
+++ b/test/sequential/test-inspector-port-cluster.js
@@ -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) {
@@ -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') {
@@ -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') {
@@ -287,9 +288,9 @@ function masterProcessMain() {
cluster.setupMaster(clusterSettings);
- assert.throws(() => {
+ common.expectsError(() => {
cluster.fork(params).on('exit', common.mustCall(checkExitCode));
- }, TypeError);
+ }, badPortError);
return;
}