Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errors, readline: migrate to use internal/errors.js #11390

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,12 @@ an argument of the wrong type has been passed to a Node.js API.
The `'ERR_INVALID_CALLBACK'` error code is used generically to identify that
a callback function is required and has not been provided to a Node.js API.

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

The `'ERR_INVALID_CURSOR_POS'` is thrown specifically when a cursor on a given
stream is attempted to move to a specified row without a specified column.

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

Expand Down
2 changes: 2 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ E('ERR_INDEX_OUT_OF_RANGE', 'Index out of range');
E('ERR_INVALID_ARG_TYPE', invalidArgType);
E('ERR_INVALID_CALLBACK', 'callback must be a function');
E('ERR_INVALID_FD', (fd) => `"fd" must be a positive integer: ${fd}`);
E('ERR_INVALID_CURSOR_POS',
'Cannot set cursor row without setting its column');
E('ERR_INVALID_FILE_URL_HOST', 'File URL host %s');
E('ERR_INVALID_FILE_URL_PATH', 'File URL path %s');
E('ERR_INVALID_HANDLE_TYPE', 'This handle type cannot be sent');
Expand Down
18 changes: 14 additions & 4 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

'use strict';

const errors = require('internal/errors');
const { debug, inherits } = require('util');
const Buffer = require('buffer').Buffer;
const EventEmitter = require('events');
Expand Down Expand Up @@ -95,7 +96,7 @@ function Interface(input, output, completer, terminal) {
}

if (completer && typeof completer !== 'function') {
throw new TypeError('Argument "completer" must be a function');
throw new errors.TypeError('ERR_INVALID_OPT_VALUE', 'completer', completer);
}

if (historySize === undefined) {
Expand All @@ -105,7 +106,11 @@ function Interface(input, output, completer, terminal) {
if (typeof historySize !== 'number' ||
isNaN(historySize) ||
historySize < 0) {
throw new TypeError('Argument "historySize" must be a positive number');
throw new errors.RangeError(
'ERR_INVALID_OPT_VALUE',
'historySize',
historySize
);
}

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

Interface.prototype._writeToOutput = function _writeToOutput(stringToWrite) {
if (typeof stringToWrite !== 'string')
throw new TypeError('"stringToWrite" argument must be a string');
throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE',
'stringToWrite',
'string',
stringToWrite
);

if (this.output !== null && this.output !== undefined)
this.output.write(stringToWrite);
Expand Down Expand Up @@ -1056,7 +1066,7 @@ function cursorTo(stream, x, y) {
return;

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

if (typeof y !== 'number') {
stream.write(CSI`${x + 1}G`);
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-readline-csi.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ assert.throws(
() => readline.cursorTo(writable, 'a', 1),
common.expectsError({
type: Error,
message: /^Can't set cursor row without also setting it's column$/
code: 'ERR_INVALID_CURSOR_POS',
message: 'Cannot set cursor row without setting its column'
}));
assert.strictEqual(writable.data, '');

Expand Down
12 changes: 4 additions & 8 deletions test/parallel/test-readline-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,10 @@ function isWarned(emitter) {
input: fi,
completer: 'string is not valid'
});
}, function(err) {
if (err instanceof TypeError) {
if (/Argument "completer" must be a function/.test(err)) {
return true;
}
}
return false;
});
}, common.expectsError({
type: TypeError,
code: 'ERR_INVALID_OPT_VALUE'
}));

// duplicate lines are removed from history when
// `options.removeHistoryDuplicates` is `true`
Expand Down