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,tty: migrate to use internal/errors.js #13240

Closed
wants to merge 1 commit into from
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
1 change: 1 addition & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ E('ERR_HTTP_INVALID_STATUS_CODE',
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_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
6 changes: 3 additions & 3 deletions lib/tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const TTY = process.binding('tty_wrap').TTY;
const isTTY = process.binding('tty_wrap').isTTY;
const inherits = util.inherits;
const errnoException = util._errnoException;

const errors = require('internal/errors');

exports.isatty = function(fd) {
return isTTY(fd);
Expand All @@ -38,7 +38,7 @@ function ReadStream(fd, options) {
if (!(this instanceof ReadStream))
return new ReadStream(fd, options);
if (fd >> 0 !== fd || fd < 0)
throw new RangeError('fd must be positive integer: ' + fd);
throw new errors.RangeError('ERR_INVALID_FD', fd);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're passing the fd in as an argument but you're not using it in the error message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasnell fixed as per your suggestion


options = util._extend({
highWaterMark: 0,
Expand Down Expand Up @@ -67,7 +67,7 @@ function WriteStream(fd) {
if (!(this instanceof WriteStream))
return new WriteStream(fd);
if (fd >> 0 !== fd || fd < 0)
throw new RangeError('fd must be positive integer: ' + fd);
throw new errors.RangeError('ERR_INVALID_FD', fd);

net.Socket.call(this, {
handle: new TTY(fd, false),
Expand Down
16 changes: 12 additions & 4 deletions test/parallel/test-ttywrap-invalid-fd.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const tty = require('tty');


assert.throws(() => {
new tty.WriteStream(-1);
}, /fd must be positive integer:/);
}, common.expectsError({
code: 'ERR_INVALID_FD',
type: RangeError,
message: '"fd" must be a positive integer: -1'
})
);

const err_regex = common.isWindows ?
/^Error: EBADF: bad file descriptor, uv_tty_init$/ :
Expand All @@ -24,7 +27,12 @@ assert.throws(() => {

assert.throws(() => {
new tty.ReadStream(-1);
}, /fd must be positive integer:/);
}, common.expectsError({
code: 'ERR_INVALID_FD',
type: RangeError,
message: '"fd" must be a positive integer: -1'
})
);

assert.throws(() => {
let fd = 2;
Expand Down