Skip to content

Commit 7489141

Browse files
committed
zlib: migrate to internal/errors
PR-URL: #15618 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 1f8d527 commit 7489141

9 files changed

+231
-101
lines changed

doc/api/errors.md

+18
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,12 @@ also able to define their own types when using the public embedder API.
609609

610610
Used when attempting to perform an operation outside the bounds of a `Buffer`.
611611

612+
<a id="ERR_BUFFER_TOO_LARGE"></a>
613+
### ERR_BUFFER_TOO_LARGE
614+
615+
Used when an attempt has been made to create a `Buffer` larger than the
616+
maximum allowed size.
617+
612618
<a id="ERR_CHILD_CLOSED_BEFORE_REPLY"></a>
613619
### ERR_CHILD_CLOSED_BEFORE_REPLY
614620

@@ -879,6 +885,12 @@ Used when a given index is out of the accepted range (e.g. negative offsets).
879885
Used generically to identify that an argument of the wrong type has been passed
880886
to a Node.js API.
881887

888+
<a id="ERR_INVALID_ARG_VALUE"></a>
889+
### ERR_INVALID_ARG_VALUE
890+
891+
Used generically to identify that an invalid or unsupported value has been
892+
passed for a given argument.
893+
882894
<a id="ERR_INVALID_ARRAY_LENGTH"></a>
883895
### ERR_INVALID_ARRAY_LENGTH
884896

@@ -1277,6 +1289,12 @@ entry types were found.
12771289

12781290
Used when a given value is out of the accepted range.
12791291

1292+
<a id="ERR_ZLIB_BINDING_CLOSED"></a>
1293+
### ERR_ZLIB_BINDING_CLOSED
1294+
1295+
Used when an attempt is made to use a `zlib` object after it has already been
1296+
closed.
1297+
12801298
[`ERR_INVALID_ARG_TYPE`]: #ERR_INVALID_ARG_TYPE
12811299
[`subprocess.kill()`]: child_process.html#child_process_subprocess_kill_signal
12821300
[`subprocess.send()`]: child_process.html#child_process_subprocess_send_message_sendhandle_options_callback

lib/internal/errors.js

+11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
const kCode = Symbol('code');
1212
const messages = new Map();
1313

14+
const {
15+
kMaxLength
16+
} = process.binding('buffer');
17+
1418
// Lazily loaded
1519
var util = null;
1620

@@ -121,6 +125,8 @@ E('ERR_ASSERTION', '%s');
121125
E('ERR_ASYNC_CALLBACK', (name) => `${name} must be a function`);
122126
E('ERR_ASYNC_TYPE', (s) => `Invalid name for async "type": ${s}`);
123127
E('ERR_BUFFER_OUT_OF_BOUNDS', bufferOutOfBounds);
128+
E('ERR_BUFFER_TOO_LARGE',
129+
`Cannot create a Buffer larger than 0x${kMaxLength.toString(16)} bytes`);
124130
E('ERR_CHILD_CLOSED_BEFORE_REPLY', 'Child closed before reply received');
125131
E('ERR_CONSOLE_WRITABLE_STREAM',
126132
'Console expects a writable stream instance for %s');
@@ -206,6 +212,10 @@ E('ERR_HTTP_TRAILER_INVALID',
206212
'Trailers are invalid with this transfer encoding');
207213
E('ERR_INDEX_OUT_OF_RANGE', 'Index out of range');
208214
E('ERR_INVALID_ARG_TYPE', invalidArgType);
215+
E('ERR_INVALID_ARG_VALUE',
216+
(name, value) => {
217+
return `The value "${String(value)}" is invalid for argument "${name}"`;
218+
});
209219
E('ERR_INVALID_ARRAY_LENGTH',
210220
(name, len, actual) => {
211221
internalAssert(typeof actual === 'number', 'actual must be a number');
@@ -303,6 +313,7 @@ E('ERR_VALID_PERFORMANCE_ENTRY_TYPE',
303313
E('ERR_VALUE_OUT_OF_RANGE', (start, end, value) => {
304314
return `The value of "${start}" must be ${end}. Received "${value}"`;
305315
});
316+
E('ERR_ZLIB_BINDING_CLOSED', 'zlib binding closed');
306317

307318
function invalidArgType(name, expected, actual) {
308319
internalAssert(name, 'name is required');

lib/zlib.js

+33-21
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@
2121

2222
'use strict';
2323

24-
const Buffer = require('buffer').Buffer;
24+
const errors = require('internal/errors');
2525
const Transform = require('_stream_transform');
2626
const { _extend } = require('util');
2727
const { isArrayBufferView } = require('internal/util/types');
2828
const binding = process.binding('zlib');
2929
const assert = require('assert').ok;
30-
const kMaxLength = require('buffer').kMaxLength;
31-
const kRangeErrorMessage = 'Cannot create final Buffer. It would be larger ' +
32-
`than 0x${kMaxLength.toString(16)} bytes`;
30+
const {
31+
Buffer,
32+
kMaxLength
33+
} = require('buffer');
3334

3435
const constants = process.binding('constants').zlib;
3536
const {
@@ -93,7 +94,7 @@ function zlibBufferOnEnd() {
9394
var buf;
9495
var err;
9596
if (this.nread >= kMaxLength) {
96-
err = new RangeError(kRangeErrorMessage);
97+
err = new errors.RangeError('ERR_BUFFER_TOO_LARGE');
9798
} else {
9899
var bufs = this.buffers;
99100
buf = (bufs.length === 1 ? bufs[0] : Buffer.concat(bufs, this.nread));
@@ -111,8 +112,9 @@ function zlibBufferSync(engine, buffer) {
111112
if (typeof buffer === 'string') {
112113
buffer = Buffer.from(buffer);
113114
} else if (!isArrayBufferView(buffer)) {
114-
throw new TypeError('"buffer" argument must be a string, Buffer, ' +
115-
'TypedArray, or DataView');
115+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
116+
'buffer',
117+
['string', 'Buffer', 'TypedArray', 'DataView']);
116118
}
117119
buffer = processChunkSync(engine, buffer, engine._finishFlushFlag);
118120
if (engine._info)
@@ -128,7 +130,7 @@ function zlibOnError(message, errno) {
128130
_close(self);
129131
self._hadError = true;
130132

131-
var error = new Error(message);
133+
const error = new Error(message);
132134
error.errno = errno;
133135
error.code = codes[errno];
134136
self.emit('error', error);
@@ -163,15 +165,17 @@ function Zlib(opts, mode) {
163165
chunkSize = opts.chunkSize;
164166
if (chunkSize !== undefined && chunkSize === chunkSize) {
165167
if (chunkSize < Z_MIN_CHUNK || !Number.isFinite(chunkSize))
166-
throw new RangeError('Invalid chunk size: ' + chunkSize);
168+
throw new errors.RangeError('ERR_INVALID_OPT_VALUE',
169+
'chunkSize',
170+
chunkSize);
167171
} else {
168172
chunkSize = Z_DEFAULT_CHUNK;
169173
}
170174

171175
flush = opts.flush;
172176
if (flush !== undefined && flush === flush) {
173177
if (flush < Z_NO_FLUSH || flush > Z_BLOCK || !Number.isFinite(flush))
174-
throw new RangeError('Invalid flush flag: ' + flush);
178+
throw new errors.RangeError('ERR_INVALID_OPT_VALUE', 'flush', flush);
175179
} else {
176180
flush = Z_NO_FLUSH;
177181
}
@@ -180,7 +184,9 @@ function Zlib(opts, mode) {
180184
if (finishFlush !== undefined && finishFlush === finishFlush) {
181185
if (finishFlush < Z_NO_FLUSH || finishFlush > Z_BLOCK ||
182186
!Number.isFinite(finishFlush)) {
183-
throw new RangeError('Invalid flush flag: ' + finishFlush);
187+
throw new errors.RangeError('ERR_INVALID_OPT_VALUE',
188+
'finishFlush',
189+
finishFlush);
184190
}
185191
} else {
186192
finishFlush = Z_FINISH;
@@ -190,7 +196,9 @@ function Zlib(opts, mode) {
190196
if (windowBits !== undefined && windowBits === windowBits) {
191197
if (windowBits < Z_MIN_WINDOWBITS || windowBits > Z_MAX_WINDOWBITS ||
192198
!Number.isFinite(windowBits)) {
193-
throw new RangeError('Invalid windowBits: ' + windowBits);
199+
throw new errors.RangeError('ERR_INVALID_OPT_VALUE',
200+
'windowBits',
201+
windowBits);
194202
}
195203
} else {
196204
windowBits = Z_DEFAULT_WINDOWBITS;
@@ -200,7 +208,8 @@ function Zlib(opts, mode) {
200208
if (level !== undefined && level === level) {
201209
if (level < Z_MIN_LEVEL || level > Z_MAX_LEVEL ||
202210
!Number.isFinite(level)) {
203-
throw new RangeError('Invalid compression level: ' + level);
211+
throw new errors.RangeError('ERR_INVALID_OPT_VALUE',
212+
'level', level);
204213
}
205214
} else {
206215
level = Z_DEFAULT_COMPRESSION;
@@ -210,7 +219,8 @@ function Zlib(opts, mode) {
210219
if (memLevel !== undefined && memLevel === memLevel) {
211220
if (memLevel < Z_MIN_MEMLEVEL || memLevel > Z_MAX_MEMLEVEL ||
212221
!Number.isFinite(memLevel)) {
213-
throw new RangeError('Invalid memLevel: ' + memLevel);
222+
throw new errors.RangeError('ERR_INVALID_OPT_VALUE',
223+
'memLevel', memLevel);
214224
}
215225
} else {
216226
memLevel = Z_DEFAULT_MEMLEVEL;
@@ -220,16 +230,18 @@ function Zlib(opts, mode) {
220230
if (strategy !== undefined && strategy === strategy) {
221231
if (strategy < Z_DEFAULT_STRATEGY || strategy > Z_FIXED ||
222232
!Number.isFinite(strategy)) {
223-
throw new TypeError('Invalid strategy: ' + strategy);
233+
throw new errors.TypeError('ERR_INVALID_OPT_VALUE',
234+
'strategy', strategy);
224235
}
225236
} else {
226237
strategy = Z_DEFAULT_STRATEGY;
227238
}
228239

229240
dictionary = opts.dictionary;
230241
if (dictionary !== undefined && !isArrayBufferView(dictionary)) {
231-
throw new TypeError(
232-
'Invalid dictionary: it should be a Buffer, TypedArray, or DataView');
242+
throw new errors.TypeError('ERR_INVALID_OPT_VALUE',
243+
'dictionary',
244+
dictionary);
233245
}
234246

235247
if (opts.encoding || opts.objectMode || opts.writableObjectMode) {
@@ -273,12 +285,12 @@ Object.defineProperty(Zlib.prototype, '_closed', {
273285

274286
Zlib.prototype.params = function params(level, strategy, callback) {
275287
if (level < Z_MIN_LEVEL || level > Z_MAX_LEVEL)
276-
throw new RangeError('Invalid compression level: ' + level);
288+
throw new errors.RangeError('ERR_INVALID_ARG_VALUE', 'level', level);
277289

278290
if (strategy !== undefined &&
279291
(strategy < Z_DEFAULT_STRATEGY || strategy > Z_FIXED ||
280292
!Number.isFinite(strategy))) {
281-
throw new TypeError('Invalid strategy: ' + strategy);
293+
throw new errors.TypeError('ERR_INVALID_ARG_VALUE', 'strategy', strategy);
282294
}
283295

284296
if (this._level !== level || this._strategy !== strategy) {
@@ -455,7 +467,7 @@ function processChunkSync(self, chunk, flushFlag) {
455467

456468
if (nread >= kMaxLength) {
457469
_close(self);
458-
throw new RangeError(kRangeErrorMessage);
470+
throw new errors.RangeError('ERR_BUFFER_TOO_LARGE');
459471
}
460472

461473
_close(self);
@@ -466,7 +478,7 @@ function processChunkSync(self, chunk, flushFlag) {
466478
function processChunk(self, chunk, flushFlag, cb) {
467479
var handle = self._handle;
468480
if (!handle)
469-
return cb(new Error('zlib binding closed'));
481+
return cb(new errors.Error('ERR_ZLIB_BINDING_CLOSED'));
470482

471483
handle.buffer = chunk;
472484
handle.cb = cb;

test/parallel/test-internal-errors.js

+17
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,20 @@ assert.strictEqual(
284284
assert.strictEqual(
285285
errors.message('ERR_INVALID_ASYNC_ID', ['asyncId', undefined]),
286286
'Invalid asyncId value: undefined');
287+
288+
{
289+
const { kMaxLength } = process.binding('buffer');
290+
const error = new errors.Error('ERR_BUFFER_TOO_LARGE');
291+
assert.strictEqual(
292+
error.message,
293+
`Cannot create a Buffer larger than 0x${kMaxLength.toString(16)} bytes`
294+
);
295+
}
296+
297+
{
298+
const error = new errors.Error('ERR_INVALID_ARG_VALUE', 'foo', 'bar');
299+
assert.strictEqual(
300+
error.message,
301+
'The value "bar" is invalid for argument "foo"'
302+
);
303+
}

0 commit comments

Comments
 (0)