From 3bb5126ff0b043045141d76f1adf18c13d1048b8 Mon Sep 17 00:00:00 2001 From: Scott McKenzie Date: Tue, 14 Feb 2017 19:58:49 -0800 Subject: [PATCH 1/7] errors, readline: migrate to use internal/errors.js --- doc/api/errors.md | 6 ++++++ lib/internal/errors.js | 2 ++ lib/readline.js | 14 ++++++++++---- test/parallel/test-readline-interface.js | 9 +-------- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index e521796edd75a5..c72f6589892ba0 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -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. + +### 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. + ### ERR_INVALID_FILE_URL_HOST diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 57e73ac56a1b43..3de677b7d38ab2 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -124,6 +124,7 @@ 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', 'Can\'t 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'); @@ -150,6 +151,7 @@ E('ERR_IPC_SYNC_FORK', 'IPC cannot be used with synchronous forks'); E('ERR_MISSING_ARGS', missingArgs); E('ERR_PARSE_HISTORY_DATA', (oldHistoryPath) => `Could not parse history data in ${oldHistoryPath}`); +E('ERR_NON_NEGATIVE_NUMBER', 'number must be non-negative'); E('ERR_STDERR_CLOSE', 'process.stderr cannot be closed'); E('ERR_STDOUT_CLOSE', 'process.stdout cannot be closed'); E('ERR_UNKNOWN_BUILTIN_MODULE', (id) => `No such built-in module: ${id}`); diff --git a/lib/readline.js b/lib/readline.js index 6a1ed150d72cd2..f0b1f5c7f031e8 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -57,6 +57,7 @@ const ESCAPE_DECODER = Symbol('escape-decoder'); // GNU readline library - keyseq-timeout is 500ms (default) const ESCAPE_CODE_TIMEOUT = 500; +const errors = require('internal/errors'); function createInterface(input, output, completer, terminal) { @@ -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_CALLBACK'); } if (historySize === undefined) { @@ -105,7 +106,7 @@ 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.TypeError('ERR_NON_NEGATIVE_NUMBER'); } // backwards compat; check the isTTY prop of the output stream @@ -281,7 +282,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); @@ -1056,7 +1062,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`); diff --git a/test/parallel/test-readline-interface.js b/test/parallel/test-readline-interface.js index 6f8849a322618a..0961bd5531e2d2 100644 --- a/test/parallel/test-readline-interface.js +++ b/test/parallel/test-readline-interface.js @@ -315,14 +315,7 @@ 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('ERR_INVALID_CALLBACK', TypeError)); // duplicate lines are removed from history when // `options.removeHistoryDuplicates` is `true` From 035094c9dc99643420055c1b3027dac13e1416a2 Mon Sep 17 00:00:00 2001 From: Scott McKenzie Date: Tue, 23 May 2017 22:05:20 -0700 Subject: [PATCH 2/7] errors, readline: integrate changes from master --- lib/readline.js | 2 +- test/parallel/test-readline-csi.js | 3 ++- test/parallel/test-readline-interface.js | 6 +++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/readline.js b/lib/readline.js index f0b1f5c7f031e8..ff9b3eec3e795b 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -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'); @@ -57,7 +58,6 @@ const ESCAPE_DECODER = Symbol('escape-decoder'); // GNU readline library - keyseq-timeout is 500ms (default) const ESCAPE_CODE_TIMEOUT = 500; -const errors = require('internal/errors'); function createInterface(input, output, completer, terminal) { diff --git a/test/parallel/test-readline-csi.js b/test/parallel/test-readline-csi.js index bde37138e3b3dd..75a3a329529d79 100644 --- a/test/parallel/test-readline-csi.js +++ b/test/parallel/test-readline-csi.js @@ -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: 'Can\'t set cursor row without setting its column' })); assert.strictEqual(writable.data, ''); diff --git a/test/parallel/test-readline-interface.js b/test/parallel/test-readline-interface.js index 0961bd5531e2d2..623deace1193b9 100644 --- a/test/parallel/test-readline-interface.js +++ b/test/parallel/test-readline-interface.js @@ -315,7 +315,11 @@ function isWarned(emitter) { input: fi, completer: 'string is not valid' }); - }, common.expectsError('ERR_INVALID_CALLBACK', TypeError)); + }, common.expectsError({ + type: TypeError, + code: 'ERR_INVALID_CALLBACK', + message: 'callback must be a function' + })); // duplicate lines are removed from history when // `options.removeHistoryDuplicates` is `true` From 7cf13a06751afd8cacbbe861b0819111a5116134 Mon Sep 17 00:00:00 2001 From: Scott McKenzie Date: Tue, 23 May 2017 23:01:15 -0700 Subject: [PATCH 3/7] errors: readline: throw more accurate errors Makes use of `ERR_INVALID_OPT_VALUE` and removes an unused error code. --- lib/internal/errors.js | 1 - lib/readline.js | 8 ++++++-- test/parallel/test-readline-interface.js | 3 +-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 3de677b7d38ab2..801a751bc57df7 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -151,7 +151,6 @@ E('ERR_IPC_SYNC_FORK', 'IPC cannot be used with synchronous forks'); E('ERR_MISSING_ARGS', missingArgs); E('ERR_PARSE_HISTORY_DATA', (oldHistoryPath) => `Could not parse history data in ${oldHistoryPath}`); -E('ERR_NON_NEGATIVE_NUMBER', 'number must be non-negative'); E('ERR_STDERR_CLOSE', 'process.stderr cannot be closed'); E('ERR_STDOUT_CLOSE', 'process.stdout cannot be closed'); E('ERR_UNKNOWN_BUILTIN_MODULE', (id) => `No such built-in module: ${id}`); diff --git a/lib/readline.js b/lib/readline.js index ff9b3eec3e795b..408d84825adb83 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -96,7 +96,7 @@ function Interface(input, output, completer, terminal) { } if (completer && typeof completer !== 'function') { - throw new errors.TypeError('ERR_INVALID_CALLBACK'); + throw new errors.TypeError('ERR_INVALID_OPT_VALUE', 'completer', completer); } if (historySize === undefined) { @@ -106,7 +106,11 @@ function Interface(input, output, completer, terminal) { if (typeof historySize !== 'number' || isNaN(historySize) || historySize < 0) { - throw new errors.TypeError('ERR_NON_NEGATIVE_NUMBER'); + throw new errors.RangeError( + 'ERR_INVALID_OPT_VALUE', + 'historySize', + historySize + ); } // backwards compat; check the isTTY prop of the output stream diff --git a/test/parallel/test-readline-interface.js b/test/parallel/test-readline-interface.js index 623deace1193b9..5d77119cdd2341 100644 --- a/test/parallel/test-readline-interface.js +++ b/test/parallel/test-readline-interface.js @@ -317,8 +317,7 @@ function isWarned(emitter) { }); }, common.expectsError({ type: TypeError, - code: 'ERR_INVALID_CALLBACK', - message: 'callback must be a function' + code: 'ERR_INVALID_OPT_VALUE' })); // duplicate lines are removed from history when From 88b663fc32ab80912adffe2aa7a77c88a103ee5c Mon Sep 17 00:00:00 2001 From: Scott McKenzie Date: Tue, 30 May 2017 17:40:05 -0700 Subject: [PATCH 4/7] errors, readline: revert accidental whitespace change --- doc/api/errors.md | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/api/errors.md b/doc/api/errors.md index c72f6589892ba0..682b5da6b705c4 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -559,6 +559,7 @@ found [here][online]. encountered by [`http`][] or [`net`][] -- often a sign that a `socket.end()` was not properly called. + ## Node.js Error Codes From 9c6045f7634f0f600cc0023a312002e08c930ed2 Mon Sep 17 00:00:00 2001 From: Scott McKenzie Date: Tue, 30 May 2017 17:42:18 -0700 Subject: [PATCH 5/7] errors, readline: avoid contraction in error message --- doc/api/errors.md | 1 - lib/internal/errors.js | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index 682b5da6b705c4..c72f6589892ba0 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -559,7 +559,6 @@ found [here][online]. encountered by [`http`][] or [`net`][] -- often a sign that a `socket.end()` was not properly called. - ## Node.js Error Codes diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 801a751bc57df7..88b00e0df0a38a 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -124,7 +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', 'Can\'t set cursor row without setting its column'); +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'); From c238fb97ca023bef5aa57139a409de309d345de5 Mon Sep 17 00:00:00 2001 From: Scott McKenzie Date: Tue, 30 May 2017 23:00:34 -0700 Subject: [PATCH 6/7] errors, readline: fix lint error --- lib/internal/errors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 88b00e0df0a38a..18cde30b696209 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -125,7 +125,7 @@ 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'); + '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'); From 4041b9735b4228e86d89db498653ea9021813a1b Mon Sep 17 00:00:00 2001 From: Scott McKenzie Date: Tue, 30 May 2017 23:01:10 -0700 Subject: [PATCH 7/7] errors, readline: pass tests --- test/parallel/test-readline-csi.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-readline-csi.js b/test/parallel/test-readline-csi.js index 75a3a329529d79..d83948764314de 100644 --- a/test/parallel/test-readline-csi.js +++ b/test/parallel/test-readline-csi.js @@ -78,7 +78,7 @@ assert.throws( common.expectsError({ type: Error, code: 'ERR_INVALID_CURSOR_POS', - message: 'Can\'t set cursor row without setting its column' + message: 'Cannot set cursor row without setting its column' })); assert.strictEqual(writable.data, '');