diff options
author | James M Snell <jasnell@gmail.com> | 2017-10-27 17:20:54 -0700 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2017-11-02 11:58:38 -0700 |
commit | 3d9d84940ae11a026c8f14e1b502794db7538765 (patch) | |
tree | 634619ce40599ea3da38b37398314dfeceecd7cf | |
parent | 056b858e57a72c5429d9278bc7b77d1922a2dbe4 (diff) | |
download | android-node-v8-3d9d84940ae11a026c8f14e1b502794db7538765.tar.gz android-node-v8-3d9d84940ae11a026c8f14e1b502794db7538765.tar.bz2 android-node-v8-3d9d84940ae11a026c8f14e1b502794db7538765.zip |
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>
-rw-r--r-- | lib/internal/errors.js | 13 | ||||
-rw-r--r-- | lib/tty.js | 16 | ||||
-rw-r--r-- | src/tty_wrap.cc | 7 | ||||
-rw-r--r-- | test/parallel/test-ttywrap-invalid-fd.js | 83 |
4 files changed, 77 insertions, 42 deletions
diff --git a/lib/internal/errors.js b/lib/internal/errors.js index d6a77b8875..c871f8e8d3 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -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; } } diff --git a/lib/tty.js b/lib/tty.js index cf020f529d..144fc86b8e 100644 --- a/lib/tty.js +++ b/lib/tty.js @@ -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 }); diff --git a/src/tty_wrap.cc b/src/tty_wrap.cc index 872a126c6d..4d49e1a65c 100644 --- a/src/tty_wrap.cc +++ b/src/tty_wrap.cc @@ -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(); } diff --git a/test/parallel/test-ttywrap-invalid-fd.js b/test/parallel/test-ttywrap-invalid-fd.js index 44647590cc..ad7a98da34 100644 --- a/test/parallel/test-ttywrap-invalid-fd.js +++ b/test/parallel/test-ttywrap-invalid-fd.js @@ -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' + } +); |