Skip to content

Commit

Permalink
tty: convert to internal/errors using SystemError
Browse files Browse the repository at this point in the history
PR-URL: #16567
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
  • Loading branch information
jasnell committed Nov 2, 2017
1 parent 056b858 commit 3d9d849
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 42 deletions.
13 changes: 11 additions & 2 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const { defineProperty } = Object;

// Lazily loaded
var util = null;
var buffer;

function makeNodeError(Base) {
return class NodeError extends Base {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand All @@ -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;
}
}

Expand Down
16 changes: 14 additions & 2 deletions lib/tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
});
Expand Down
7 changes: 4 additions & 3 deletions src/tty_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
83 changes: 48 additions & 35 deletions test/parallel/test-ttywrap-invalid-fd.js
Original file line number Diff line number Diff line change
@@ -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
});
}

assert.throws(() => {
let fd = 2;
// Get first known bad file descriptor.
try {
while (fs.fstatSync(++fd));
} catch (e) { }
new tty.ReadStream(fd);
}, err_regex);
common.expectsError(
() => new tty.ReadStream(-1),
{
code: 'ERR_INVALID_FD',
type: RangeError,
message: '"fd" must be a positive integer: -1'
}
);

0 comments on commit 3d9d849

Please sign in to comment.