Skip to content

Commit 8f2b82a

Browse files
200GAUTAMjasnell
authored andcommitted
errors,tty: migrate to use internal/errors.js
PR-URL: #13240 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 791b5b5 commit 8f2b82a

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

lib/internal/errors.js

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ E('ERR_ARG_NOT_ITERABLE', '%s must be iterable');
114114
E('ERR_ASSERTION', (msg) => msg);
115115
E('ERR_INVALID_ARG_TYPE', invalidArgType);
116116
E('ERR_INVALID_CALLBACK', 'callback must be a function');
117+
E('ERR_INVALID_FD', (fd) => `"fd" must be a positive integer: ${fd}`);
117118
E('ERR_INVALID_FILE_URL_HOST', 'File URL host %s');
118119
E('ERR_INVALID_FILE_URL_PATH', 'File URL path %s');
119120
E('ERR_INVALID_HANDLE_TYPE', 'This handle type cannot be sent');

lib/tty.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const TTY = process.binding('tty_wrap').TTY;
2727
const isTTY = process.binding('tty_wrap').isTTY;
2828
const inherits = util.inherits;
2929
const errnoException = util._errnoException;
30-
30+
const errors = require('internal/errors');
3131

3232
exports.isatty = function(fd) {
3333
return isTTY(fd);
@@ -38,7 +38,7 @@ function ReadStream(fd, options) {
3838
if (!(this instanceof ReadStream))
3939
return new ReadStream(fd, options);
4040
if (fd >> 0 !== fd || fd < 0)
41-
throw new RangeError('fd must be positive integer: ' + fd);
41+
throw new errors.RangeError('ERR_INVALID_FD', fd);
4242

4343
options = util._extend({
4444
highWaterMark: 0,
@@ -67,7 +67,7 @@ function WriteStream(fd) {
6767
if (!(this instanceof WriteStream))
6868
return new WriteStream(fd);
6969
if (fd >> 0 !== fd || fd < 0)
70-
throw new RangeError('fd must be positive integer: ' + fd);
70+
throw new errors.RangeError('ERR_INVALID_FD', fd);
7171

7272
net.Socket.call(this, {
7373
handle: new TTY(fd, false),

test/parallel/test-ttywrap-invalid-fd.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
'use strict';
2-
32
const common = require('../common');
43
const assert = require('assert');
54
const fs = require('fs');
65
const tty = require('tty');
76

8-
97
assert.throws(() => {
108
new tty.WriteStream(-1);
11-
}, /fd must be positive integer:/);
9+
}, common.expectsError({
10+
code: 'ERR_INVALID_FD',
11+
type: RangeError,
12+
message: '"fd" must be a positive integer: -1'
13+
})
14+
);
1215

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

2528
assert.throws(() => {
2629
new tty.ReadStream(-1);
27-
}, /fd must be positive integer:/);
30+
}, common.expectsError({
31+
code: 'ERR_INVALID_FD',
32+
type: RangeError,
33+
message: '"fd" must be a positive integer: -1'
34+
})
35+
);
2836

2937
assert.throws(() => {
3038
let fd = 2;

0 commit comments

Comments
 (0)