diff --git a/lib/child_process.js b/lib/child_process.js index d17329e3811..b5b44bc379b 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -28,6 +28,7 @@ var assert = require('assert'); var util = require('util'); var constants; // if (!constants) constants = process.binding('constants'); +var errnoException = util._errnoException; var handleWraps = {}; function handleWrapGetter(name, callback) { @@ -969,21 +970,6 @@ ChildProcess.prototype.spawn = function(options) { }; -function errnoException(errorno, syscall, errmsg) { - // TODO make this more compatible with ErrnoException from src/node.cc - // Once all of Node is using this function the ErrnoException from - // src/node.cc should be removed. - var message = syscall + ' ' + errorno; - if (errmsg) { - message += ' - ' + errmsg; - } - var e = new Error(message); - e.errno = e.code = errorno; - e.syscall = syscall; - return e; -} - - ChildProcess.prototype.kill = function(sig) { var signal; diff --git a/lib/dgram.js b/lib/dgram.js index 7dd2086d7a8..570cbbe8dd6 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -34,6 +34,7 @@ var cluster = null; var dns = null; var net = null; +var errnoException = util._errnoException; // no-op callback function noop() { @@ -434,11 +435,3 @@ Socket.prototype.unref = function() { if (this._handle) this._handle.unref(); }; - -// TODO share with net_uv and others -function errnoException(errorno, syscall) { - var e = new Error(syscall + ' ' + errorno); - e.errno = e.code = errorno; - e.syscall = syscall; - return e; -} diff --git a/lib/dns.js b/lib/dns.js index e9611a3a71a..baac2b48d9f 100644 --- a/lib/dns.js +++ b/lib/dns.js @@ -19,27 +19,12 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. -var cares = process.binding('cares_wrap'), - net = require('net'), - isIp = net.isIP; +var cares = process.binding('cares_wrap'); +var net = require('net'); +var util = require('util'); - -function errnoException(errorno, syscall) { - // TODO make this more compatible with ErrnoException from src/node.cc - // Once all of Node is using this function the ErrnoException from - // src/node.cc should be removed. - - // For backwards compatibility. libuv returns ENOENT on NXDOMAIN. - if (errorno == 'ENOENT') { - errorno = 'ENOTFOUND'; - } - - var e = new Error(syscall + ' ' + errorno); - - e.errno = e.code = errorno; - e.syscall = syscall; - return e; -} +var errnoException = util._errnoException; +var isIp = net.isIP; // c-ares invokes a callback either synchronously or asynchronously, diff --git a/lib/fs.js b/lib/fs.js index b6cc49036f3..01a3c4d5e98 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -55,6 +55,8 @@ var O_WRONLY = constants.O_WRONLY || 0; var isWindows = process.platform === 'win32'; var DEBUG = process.env.NODE_DEBUG && /fs/.test(process.env.NODE_DEBUG); +var errnoException = util._errnoException; + function rethrow() { // Only enable in debug mode. A backtrace uses ~1000 bytes of heap space and @@ -334,12 +336,6 @@ function stringToFlags(flag) { return flag; } - // O_EXCL is mandated by POSIX, Windows supports it too. - // Let's add a check anyway, just in case. - if (!O_EXCL && ~flag.indexOf('x')) { - throw errnoException('ENOSYS', 'fs.open(O_EXCL)'); - } - switch (flag) { case 'r' : return O_RDONLY; case 'rs' : return O_RDONLY | O_SYNC; @@ -999,17 +995,6 @@ fs.appendFileSync = function(path, data, options) { fs.writeFileSync(path, data, options); }; -function errnoException(errorno, syscall) { - // TODO make this more compatible with ErrnoException from src/node.cc - // Once all of Node is using this function the ErrnoException from - // src/node.cc should be removed. - var e = new Error(syscall + ' ' + errorno); - e.errno = e.code = errorno; - e.syscall = syscall; - return e; -} - - function FSWatcher() { EventEmitter.call(this); diff --git a/lib/net.js b/lib/net.js index d30c002d49c..c970595b50a 100644 --- a/lib/net.js +++ b/lib/net.js @@ -25,7 +25,9 @@ var timers = require('timers'); var util = require('util'); var assert = require('assert'); var cares = process.binding('cares_wrap'); + var cluster; +var errnoException = util._errnoException; function noop() {} @@ -940,19 +942,6 @@ function afterConnect(status, handle, req, readable, writable) { } -function errnoException(errorno, syscall) { - // TODO make this more compatible with ErrnoException from src/node.cc - // Once all of Node is using this function the ErrnoException from - // src/node.cc should be removed. - var e = new Error(syscall + ' ' + errorno); - e.errno = e.code = errorno; - e.syscall = syscall; - return e; -} - - - - function Server(/* [ options, ] listener */) { if (!(this instanceof Server)) return new Server(arguments[0], arguments[1]); events.EventEmitter.call(this); diff --git a/lib/tty.js b/lib/tty.js index 98d498a17c4..5d60ec6608c 100644 --- a/lib/tty.js +++ b/lib/tty.js @@ -26,6 +26,9 @@ var TTY = process.binding('tty_wrap').TTY; var isTTY = process.binding('tty_wrap').isTTY; var util = require('util'); +var errnoException = util._errnoException; + + exports.isatty = function(fd) { return isTTY(fd); }; @@ -123,12 +126,3 @@ WriteStream.prototype.clearScreenDown = function() { WriteStream.prototype.getWindowSize = function() { return [this.columns, this.rows]; }; - - -// TODO share with net_uv and others -function errnoException(errorno, syscall) { - var e = new Error(syscall + ' ' + errorno); - e.errno = e.code = errorno; - e.syscall = syscall; - return e; -} diff --git a/lib/util.js b/lib/util.js index 4f9d06943e0..f02aa8f6a4d 100644 --- a/lib/util.js +++ b/lib/util.js @@ -601,3 +601,15 @@ exports.pump = exports.deprecate(function(readStream, writeStream, callback) { call(err); }); }, 'util.pump(): Use readableStream.pipe() instead'); + + +var uv; +exports._errnoException = function(err, syscall) { + if (typeof uv === 'undefined') uv = process.binding('uv'); + var errname = uv.errname(err); + var e = new Error(syscall + ' ' + errname); + e.code = errname; + e.errno = errname; + e.syscall = syscall; + return e; +}; diff --git a/src/node.js b/src/node.js index 66c8cbbe54f..061cbf9c3c1 100644 --- a/src/node.js +++ b/src/node.js @@ -459,16 +459,6 @@ if (process._print_eval) console.log(result); } - function errnoException(errorno, syscall) { - // TODO make this more compatible with ErrnoException from src/node.cc - // Once all of Node is using this function the ErrnoException from - // src/node.cc should be removed. - var e = new Error(syscall + ' ' + errorno); - e.errno = e.code = errorno; - e.syscall = syscall; - return e; - } - function createWritableStdioStream(fd) { var stream; var tty_wrap = process.binding('tty_wrap'); @@ -651,6 +641,7 @@ } if (r) { + var errnoException = NativeModule.require('util')._errnoException; throw errnoException(process._errno, 'kill'); } @@ -685,6 +676,7 @@ var r = wrap.start(signum); if (r) { wrap.close(); + var errnoException = NativeModule.require('util')._errnoException; throw errnoException(process._errno, 'uv_signal_start'); }