Skip to content

Commit 3d9d849

Browse files
committed
tty: convert to internal/errors using SystemError
PR-URL: #16567 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
1 parent 056b858 commit 3d9d849

File tree

4 files changed

+77
-42
lines changed

4 files changed

+77
-42
lines changed

lib/internal/errors.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const { defineProperty } = Object;
1818

1919
// Lazily loaded
2020
var util = null;
21+
var buffer;
2122

2223
function makeNodeError(Base) {
2324
return class NodeError extends Base {
@@ -59,6 +60,12 @@ function makeNodeError(Base) {
5960
};
6061
}
6162

63+
function lazyBuffer() {
64+
if (buffer === undefined)
65+
buffer = require('buffer').Buffer;
66+
return buffer;
67+
}
68+
6269
// A specialized Error that includes an additional info property with
6370
// additional information about the error condition. The code key will
6471
// be extracted from the context object or the ERR_SYSTEM_ERROR default
@@ -108,7 +115,8 @@ class SystemError extends makeNodeError(Error) {
108115
}
109116

110117
set path(val) {
111-
this[kInfo].path = val ? Buffer.from(val.toString()) : undefined;
118+
this[kInfo].path = val ?
119+
lazyBuffer().from(val.toString()) : undefined;
112120
}
113121

114122
get dest() {
@@ -117,7 +125,8 @@ class SystemError extends makeNodeError(Error) {
117125
}
118126

119127
set dest(val) {
120-
this[kInfo].dest = val ? Buffer.from(val.toString()) : undefined;
128+
this[kInfo].dest = val ?
129+
lazyBuffer().from(val.toString()) : undefined;
121130
}
122131
}
123132

lib/tty.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,17 @@ function ReadStream(fd, options) {
4040
if (fd >> 0 !== fd || fd < 0)
4141
throw new errors.RangeError('ERR_INVALID_FD', fd);
4242

43+
const ctx = {};
44+
const tty = new TTY(fd, true, ctx);
45+
if (ctx.code !== undefined) {
46+
throw new errors.SystemError(ctx);
47+
}
48+
4349
options = util._extend({
4450
highWaterMark: 0,
4551
readable: true,
4652
writable: false,
47-
handle: new TTY(fd, true)
53+
handle: tty
4854
}, options);
4955

5056
net.Socket.call(this, options);
@@ -69,8 +75,14 @@ function WriteStream(fd) {
6975
if (fd >> 0 !== fd || fd < 0)
7076
throw new errors.RangeError('ERR_INVALID_FD', fd);
7177

78+
const ctx = {};
79+
const tty = new TTY(fd, false, ctx);
80+
if (ctx.code !== undefined) {
81+
throw new errors.SystemError(ctx);
82+
}
83+
7284
net.Socket.call(this, {
73-
handle: new TTY(fd, false),
85+
handle: tty,
7486
readable: false,
7587
writable: true
7688
});

src/tty_wrap.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,10 @@ void TTYWrap::New(const FunctionCallbackInfo<Value>& args) {
154154

155155
int err = 0;
156156
TTYWrap* wrap = new TTYWrap(env, args.This(), fd, args[1]->IsTrue(), &err);
157-
if (err != 0)
158-
return env->ThrowUVException(err, "uv_tty_init");
159-
157+
if (err != 0) {
158+
env->CollectUVExceptionInfo(args[2], err, "uv_tty_init");
159+
args.GetReturnValue().SetUndefined();
160+
}
160161
wrap->UpdateWriteQueueSize();
161162
}
162163

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,57 @@
11
'use strict';
22
const common = require('../common');
3-
const assert = require('assert');
43
const fs = require('fs');
54
const tty = require('tty');
65

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

16-
const err_regex = common.isWindows ?
17-
/^Error: EBADF: bad file descriptor, uv_tty_init$/ :
18-
/^Error: EINVAL: invalid argument, uv_tty_init$/;
19-
assert.throws(() => {
20-
let fd = 2;
21-
// Get first known bad file descriptor.
22-
try {
23-
while (fs.fstatSync(++fd));
24-
} catch (e) { }
25-
new tty.WriteStream(fd);
26-
}, err_regex);
15+
{
16+
const message = common.isWindows ?
17+
'bad file descriptor: EBADF [uv_tty_init]' :
18+
'invalid argument: EINVAL [uv_tty_init]';
2719

28-
assert.throws(() => {
29-
new tty.ReadStream(-1);
30-
}, common.expectsError({
31-
code: 'ERR_INVALID_FD',
32-
type: RangeError,
33-
message: '"fd" must be a positive integer: -1'
34-
})
35-
);
20+
common.expectsError(
21+
() => {
22+
let fd = 2;
23+
// Get first known bad file descriptor.
24+
try {
25+
while (fs.fstatSync(++fd));
26+
} catch (e) { }
27+
new tty.WriteStream(fd);
28+
}, {
29+
code: 'ERR_SYSTEM_ERROR',
30+
type: Error,
31+
message
32+
}
33+
);
34+
35+
common.expectsError(
36+
() => {
37+
let fd = 2;
38+
// Get first known bad file descriptor.
39+
try {
40+
while (fs.fstatSync(++fd));
41+
} catch (e) { }
42+
new tty.ReadStream(fd);
43+
}, {
44+
code: 'ERR_SYSTEM_ERROR',
45+
type: Error,
46+
message
47+
});
48+
}
3649

37-
assert.throws(() => {
38-
let fd = 2;
39-
// Get first known bad file descriptor.
40-
try {
41-
while (fs.fstatSync(++fd));
42-
} catch (e) { }
43-
new tty.ReadStream(fd);
44-
}, err_regex);
50+
common.expectsError(
51+
() => new tty.ReadStream(-1),
52+
{
53+
code: 'ERR_INVALID_FD',
54+
type: RangeError,
55+
message: '"fd" must be a positive integer: -1'
56+
}
57+
);

0 commit comments

Comments
 (0)