tty: convert to internal/errors using SystemError
PR-URL: https://github.com/nodejs/node/pull/16567 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
This commit is contained in:
parent
056b858e57
commit
3d9d84940a
@ -18,6 +18,7 @@ const { defineProperty } = Object;
|
|||||||
|
|
||||||
// Lazily loaded
|
// Lazily loaded
|
||||||
var util = null;
|
var util = null;
|
||||||
|
var buffer;
|
||||||
|
|
||||||
function makeNodeError(Base) {
|
function makeNodeError(Base) {
|
||||||
return class NodeError extends Base {
|
return class NodeError extends Base {
|
||||||
@ -59,6 +60,12 @@ function makeNodeError(Base) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function lazyBuffer() {
|
||||||
|
if (buffer === undefined)
|
||||||
|
buffer = require('buffer').Buffer;
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
// A specialized Error that includes an additional info property with
|
// A specialized Error that includes an additional info property with
|
||||||
// additional information about the error condition. The code key will
|
// additional information about the error condition. The code key will
|
||||||
// be extracted from the context object or the ERR_SYSTEM_ERROR default
|
// be extracted from the context object or the ERR_SYSTEM_ERROR default
|
||||||
@ -108,7 +115,8 @@ class SystemError extends makeNodeError(Error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
set path(val) {
|
set path(val) {
|
||||||
this[kInfo].path = val ? Buffer.from(val.toString()) : undefined;
|
this[kInfo].path = val ?
|
||||||
|
lazyBuffer().from(val.toString()) : undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
get dest() {
|
get dest() {
|
||||||
@ -117,7 +125,8 @@ class SystemError extends makeNodeError(Error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
set dest(val) {
|
set dest(val) {
|
||||||
this[kInfo].dest = val ? Buffer.from(val.toString()) : undefined;
|
this[kInfo].dest = val ?
|
||||||
|
lazyBuffer().from(val.toString()) : undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
16
lib/tty.js
16
lib/tty.js
@ -40,11 +40,17 @@ function ReadStream(fd, options) {
|
|||||||
if (fd >> 0 !== fd || fd < 0)
|
if (fd >> 0 !== fd || fd < 0)
|
||||||
throw new errors.RangeError('ERR_INVALID_FD', fd);
|
throw new errors.RangeError('ERR_INVALID_FD', fd);
|
||||||
|
|
||||||
|
const ctx = {};
|
||||||
|
const tty = new TTY(fd, true, ctx);
|
||||||
|
if (ctx.code !== undefined) {
|
||||||
|
throw new errors.SystemError(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
options = util._extend({
|
options = util._extend({
|
||||||
highWaterMark: 0,
|
highWaterMark: 0,
|
||||||
readable: true,
|
readable: true,
|
||||||
writable: false,
|
writable: false,
|
||||||
handle: new TTY(fd, true)
|
handle: tty
|
||||||
}, options);
|
}, options);
|
||||||
|
|
||||||
net.Socket.call(this, options);
|
net.Socket.call(this, options);
|
||||||
@ -69,8 +75,14 @@ function WriteStream(fd) {
|
|||||||
if (fd >> 0 !== fd || fd < 0)
|
if (fd >> 0 !== fd || fd < 0)
|
||||||
throw new errors.RangeError('ERR_INVALID_FD', fd);
|
throw new errors.RangeError('ERR_INVALID_FD', fd);
|
||||||
|
|
||||||
|
const ctx = {};
|
||||||
|
const tty = new TTY(fd, false, ctx);
|
||||||
|
if (ctx.code !== undefined) {
|
||||||
|
throw new errors.SystemError(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
net.Socket.call(this, {
|
net.Socket.call(this, {
|
||||||
handle: new TTY(fd, false),
|
handle: tty,
|
||||||
readable: false,
|
readable: false,
|
||||||
writable: true
|
writable: true
|
||||||
});
|
});
|
||||||
|
@ -154,9 +154,10 @@ void TTYWrap::New(const FunctionCallbackInfo<Value>& args) {
|
|||||||
|
|
||||||
int err = 0;
|
int err = 0;
|
||||||
TTYWrap* wrap = new TTYWrap(env, args.This(), fd, args[1]->IsTrue(), &err);
|
TTYWrap* wrap = new TTYWrap(env, args.This(), fd, args[1]->IsTrue(), &err);
|
||||||
if (err != 0)
|
if (err != 0) {
|
||||||
return env->ThrowUVException(err, "uv_tty_init");
|
env->CollectUVExceptionInfo(args[2], err, "uv_tty_init");
|
||||||
|
args.GetReturnValue().SetUndefined();
|
||||||
|
}
|
||||||
wrap->UpdateWriteQueueSize();
|
wrap->UpdateWriteQueueSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,44 +1,57 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
const common = require('../common');
|
const common = require('../common');
|
||||||
const assert = require('assert');
|
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const tty = require('tty');
|
const tty = require('tty');
|
||||||
|
|
||||||
assert.throws(() => {
|
common.expectsError(
|
||||||
new tty.WriteStream(-1);
|
() => new tty.WriteStream(-1),
|
||||||
}, common.expectsError({
|
{
|
||||||
code: 'ERR_INVALID_FD',
|
code: 'ERR_INVALID_FD',
|
||||||
type: RangeError,
|
type: RangeError,
|
||||||
message: '"fd" must be a positive integer: -1'
|
message: '"fd" must be a positive integer: -1'
|
||||||
})
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
const err_regex = common.isWindows ?
|
{
|
||||||
/^Error: EBADF: bad file descriptor, uv_tty_init$/ :
|
const message = common.isWindows ?
|
||||||
/^Error: EINVAL: invalid argument, uv_tty_init$/;
|
'bad file descriptor: EBADF [uv_tty_init]' :
|
||||||
assert.throws(() => {
|
'invalid argument: EINVAL [uv_tty_init]';
|
||||||
let fd = 2;
|
|
||||||
// Get first known bad file descriptor.
|
|
||||||
try {
|
|
||||||
while (fs.fstatSync(++fd));
|
|
||||||
} catch (e) { }
|
|
||||||
new tty.WriteStream(fd);
|
|
||||||
}, err_regex);
|
|
||||||
|
|
||||||
assert.throws(() => {
|
common.expectsError(
|
||||||
new tty.ReadStream(-1);
|
() => {
|
||||||
}, common.expectsError({
|
let fd = 2;
|
||||||
code: 'ERR_INVALID_FD',
|
// Get first known bad file descriptor.
|
||||||
type: RangeError,
|
try {
|
||||||
message: '"fd" must be a positive integer: -1'
|
while (fs.fstatSync(++fd));
|
||||||
})
|
} catch (e) { }
|
||||||
|
new tty.WriteStream(fd);
|
||||||
|
}, {
|
||||||
|
code: 'ERR_SYSTEM_ERROR',
|
||||||
|
type: Error,
|
||||||
|
message
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
common.expectsError(
|
||||||
|
() => {
|
||||||
|
let fd = 2;
|
||||||
|
// Get first known bad file descriptor.
|
||||||
|
try {
|
||||||
|
while (fs.fstatSync(++fd));
|
||||||
|
} catch (e) { }
|
||||||
|
new tty.ReadStream(fd);
|
||||||
|
}, {
|
||||||
|
code: 'ERR_SYSTEM_ERROR',
|
||||||
|
type: Error,
|
||||||
|
message
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
common.expectsError(
|
||||||
|
() => new tty.ReadStream(-1),
|
||||||
|
{
|
||||||
|
code: 'ERR_INVALID_FD',
|
||||||
|
type: RangeError,
|
||||||
|
message: '"fd" must be a positive integer: -1'
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
assert.throws(() => {
|
|
||||||
let fd = 2;
|
|
||||||
// Get first known bad file descriptor.
|
|
||||||
try {
|
|
||||||
while (fs.fstatSync(++fd));
|
|
||||||
} catch (e) { }
|
|
||||||
new tty.ReadStream(fd);
|
|
||||||
}, err_regex);
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user