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:
James M Snell 2017-10-27 17:20:54 -07:00
parent 056b858e57
commit 3d9d84940a
4 changed files with 77 additions and 42 deletions

View File

@ -18,6 +18,7 @@ const { defineProperty } = Object;
// Lazily loaded
var util = null;
var buffer;
function makeNodeError(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
// additional information about the error condition. The code key will
// be extracted from the context object or the ERR_SYSTEM_ERROR default
@ -108,7 +115,8 @@ class SystemError extends makeNodeError(Error) {
}
set path(val) {
this[kInfo].path = val ? Buffer.from(val.toString()) : undefined;
this[kInfo].path = val ?
lazyBuffer().from(val.toString()) : undefined;
}
get dest() {
@ -117,7 +125,8 @@ class SystemError extends makeNodeError(Error) {
}
set dest(val) {
this[kInfo].dest = val ? Buffer.from(val.toString()) : undefined;
this[kInfo].dest = val ?
lazyBuffer().from(val.toString()) : undefined;
}
}

View File

@ -40,11 +40,17 @@ function ReadStream(fd, options) {
if (fd >> 0 !== fd || fd < 0)
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({
highWaterMark: 0,
readable: true,
writable: false,
handle: new TTY(fd, true)
handle: tty
}, options);
net.Socket.call(this, options);
@ -69,8 +75,14 @@ function WriteStream(fd) {
if (fd >> 0 !== fd || fd < 0)
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, {
handle: new TTY(fd, false),
handle: tty,
readable: false,
writable: true
});

View File

@ -154,9 +154,10 @@ void TTYWrap::New(const FunctionCallbackInfo<Value>& args) {
int err = 0;
TTYWrap* wrap = new TTYWrap(env, args.This(), fd, args[1]->IsTrue(), &err);
if (err != 0)
return env->ThrowUVException(err, "uv_tty_init");
if (err != 0) {
env->CollectUVExceptionInfo(args[2], err, "uv_tty_init");
args.GetReturnValue().SetUndefined();
}
wrap->UpdateWriteQueueSize();
}

View File

@ -1,44 +1,57 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const tty = require('tty');
assert.throws(() => {
new tty.WriteStream(-1);
}, common.expectsError({
code: 'ERR_INVALID_FD',
type: RangeError,
message: '"fd" must be a positive integer: -1'
})
common.expectsError(
() => new tty.WriteStream(-1),
{
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$/ :
/^Error: EINVAL: invalid argument, uv_tty_init$/;
assert.throws(() => {
let fd = 2;
// Get first known bad file descriptor.
try {
while (fs.fstatSync(++fd));
} catch (e) { }
new tty.WriteStream(fd);
}, err_regex);
{
const message = common.isWindows ?
'bad file descriptor: EBADF [uv_tty_init]' :
'invalid argument: EINVAL [uv_tty_init]';
assert.throws(() => {
new tty.ReadStream(-1);
}, common.expectsError({
code: 'ERR_INVALID_FD',
type: RangeError,
message: '"fd" must be a positive integer: -1'
})
common.expectsError(
() => {
let fd = 2;
// Get first known bad file descriptor.
try {
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);