Skip to content

Commit 7f3f72c

Browse files
slammayjammayjasnell
authored andcommitted
errors, readline: migrate to use internal/errors.js
PR-URL: #11390 Ref: #11273 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent 224dbb1 commit 7f3f72c

File tree

5 files changed

+28
-13
lines changed

5 files changed

+28
-13
lines changed

doc/api/errors.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,12 @@ an argument of the wrong type has been passed to a Node.js API.
594594
The `'ERR_INVALID_CALLBACK'` error code is used generically to identify that
595595
a callback function is required and has not been provided to a Node.js API.
596596

597+
<a id="ERR_INVALID_CURSOR_POS"></a>
598+
### ERR_INVALID_CURSOR_POS
599+
600+
The `'ERR_INVALID_CURSOR_POS'` is thrown specifically when a cursor on a given
601+
stream is attempted to move to a specified row without a specified column.
602+
597603
<a id="ERR_INVALID_FILE_URL_HOST"></a>
598604
### ERR_INVALID_FILE_URL_HOST
599605

lib/internal/errors.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ E('ERR_INDEX_OUT_OF_RANGE', 'Index out of range');
124124
E('ERR_INVALID_ARG_TYPE', invalidArgType);
125125
E('ERR_INVALID_CALLBACK', 'callback must be a function');
126126
E('ERR_INVALID_FD', (fd) => `"fd" must be a positive integer: ${fd}`);
127+
E('ERR_INVALID_CURSOR_POS',
128+
'Cannot set cursor row without setting its column');
127129
E('ERR_INVALID_FILE_URL_HOST', 'File URL host %s');
128130
E('ERR_INVALID_FILE_URL_PATH', 'File URL path %s');
129131
E('ERR_INVALID_HANDLE_TYPE', 'This handle type cannot be sent');

lib/readline.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
'use strict';
2929

30+
const errors = require('internal/errors');
3031
const { debug, inherits } = require('util');
3132
const Buffer = require('buffer').Buffer;
3233
const EventEmitter = require('events');
@@ -95,7 +96,7 @@ function Interface(input, output, completer, terminal) {
9596
}
9697

9798
if (completer && typeof completer !== 'function') {
98-
throw new TypeError('Argument "completer" must be a function');
99+
throw new errors.TypeError('ERR_INVALID_OPT_VALUE', 'completer', completer);
99100
}
100101

101102
if (historySize === undefined) {
@@ -105,7 +106,11 @@ function Interface(input, output, completer, terminal) {
105106
if (typeof historySize !== 'number' ||
106107
isNaN(historySize) ||
107108
historySize < 0) {
108-
throw new TypeError('Argument "historySize" must be a positive number');
109+
throw new errors.RangeError(
110+
'ERR_INVALID_OPT_VALUE',
111+
'historySize',
112+
historySize
113+
);
109114
}
110115

111116
// backwards compat; check the isTTY prop of the output stream
@@ -281,7 +286,12 @@ Interface.prototype._onLine = function(line) {
281286

282287
Interface.prototype._writeToOutput = function _writeToOutput(stringToWrite) {
283288
if (typeof stringToWrite !== 'string')
284-
throw new TypeError('"stringToWrite" argument must be a string');
289+
throw new errors.TypeError(
290+
'ERR_INVALID_ARG_TYPE',
291+
'stringToWrite',
292+
'string',
293+
stringToWrite
294+
);
285295

286296
if (this.output !== null && this.output !== undefined)
287297
this.output.write(stringToWrite);
@@ -1053,7 +1063,7 @@ function cursorTo(stream, x, y) {
10531063
return;
10541064

10551065
if (typeof x !== 'number')
1056-
throw new Error('Can\'t set cursor row without also setting it\'s column');
1066+
throw new errors.Error('ERR_INVALID_CURSOR_POS');
10571067

10581068
if (typeof y !== 'number') {
10591069
stream.write(CSI`${x + 1}G`);

test/parallel/test-readline-csi.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ assert.throws(
7777
() => readline.cursorTo(writable, 'a', 1),
7878
common.expectsError({
7979
type: Error,
80-
message: /^Can't set cursor row without also setting it's column$/
80+
code: 'ERR_INVALID_CURSOR_POS',
81+
message: 'Cannot set cursor row without setting its column'
8182
}));
8283
assert.strictEqual(writable.data, '');
8384

test/parallel/test-readline-interface.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,14 +315,10 @@ function isWarned(emitter) {
315315
input: fi,
316316
completer: 'string is not valid'
317317
});
318-
}, function(err) {
319-
if (err instanceof TypeError) {
320-
if (/Argument "completer" must be a function/.test(err)) {
321-
return true;
322-
}
323-
}
324-
return false;
325-
});
318+
}, common.expectsError({
319+
type: TypeError,
320+
code: 'ERR_INVALID_OPT_VALUE'
321+
}));
326322

327323
// duplicate lines are removed from history when
328324
// `options.removeHistoryDuplicates` is `true`

0 commit comments

Comments
 (0)