Skip to content

Commit

Permalink
errors,tty: migrate to use internal/errors.js
Browse files Browse the repository at this point in the history
PR-URL: #13240
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
  • Loading branch information
200GAUTAM authored and jasnell committed Jun 7, 2017
1 parent 791b5b5 commit 8f2b82a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ E('ERR_ARG_NOT_ITERABLE', '%s must be iterable');
E('ERR_ASSERTION', (msg) => msg);
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);

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

0 comments on commit 8f2b82a

Please sign in to comment.