Skip to content

Commit 0161ec8

Browse files
committed
src, lib: deduplicate errnoException
1 parent d4c14c1 commit 0161ec8

File tree

8 files changed

+28
-92
lines changed

8 files changed

+28
-92
lines changed

lib/child_process.js

+1-15
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var assert = require('assert');
2828
var util = require('util');
2929
var constants; // if (!constants) constants = process.binding('constants');
3030

31+
var errnoException = util._errnoException;
3132
var handleWraps = {};
3233

3334
function handleWrapGetter(name, callback) {
@@ -969,21 +970,6 @@ ChildProcess.prototype.spawn = function(options) {
969970
};
970971

971972

972-
function errnoException(errorno, syscall, errmsg) {
973-
// TODO make this more compatible with ErrnoException from src/node.cc
974-
// Once all of Node is using this function the ErrnoException from
975-
// src/node.cc should be removed.
976-
var message = syscall + ' ' + errorno;
977-
if (errmsg) {
978-
message += ' - ' + errmsg;
979-
}
980-
var e = new Error(message);
981-
e.errno = e.code = errorno;
982-
e.syscall = syscall;
983-
return e;
984-
}
985-
986-
987973
ChildProcess.prototype.kill = function(sig) {
988974
var signal;
989975

lib/dgram.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var cluster = null;
3434
var dns = null;
3535
var net = null;
3636

37+
var errnoException = util._errnoException;
3738

3839
// no-op callback
3940
function noop() {
@@ -434,11 +435,3 @@ Socket.prototype.unref = function() {
434435
if (this._handle)
435436
this._handle.unref();
436437
};
437-
438-
// TODO share with net_uv and others
439-
function errnoException(errorno, syscall) {
440-
var e = new Error(syscall + ' ' + errorno);
441-
e.errno = e.code = errorno;
442-
e.syscall = syscall;
443-
return e;
444-
}

lib/dns.js

+5-20
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,12 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22-
var cares = process.binding('cares_wrap'),
23-
net = require('net'),
24-
isIp = net.isIP;
22+
var cares = process.binding('cares_wrap');
23+
var net = require('net');
24+
var util = require('util');
2525

26-
27-
function errnoException(errorno, syscall) {
28-
// TODO make this more compatible with ErrnoException from src/node.cc
29-
// Once all of Node is using this function the ErrnoException from
30-
// src/node.cc should be removed.
31-
32-
// For backwards compatibility. libuv returns ENOENT on NXDOMAIN.
33-
if (errorno == 'ENOENT') {
34-
errorno = 'ENOTFOUND';
35-
}
36-
37-
var e = new Error(syscall + ' ' + errorno);
38-
39-
e.errno = e.code = errorno;
40-
e.syscall = syscall;
41-
return e;
42-
}
26+
var errnoException = util._errnoException;
27+
var isIp = net.isIP;
4328

4429

4530
// c-ares invokes a callback either synchronously or asynchronously,

lib/fs.js

+2-17
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ var O_WRONLY = constants.O_WRONLY || 0;
5555
var isWindows = process.platform === 'win32';
5656

5757
var DEBUG = process.env.NODE_DEBUG && /fs/.test(process.env.NODE_DEBUG);
58+
var errnoException = util._errnoException;
59+
5860

5961
function rethrow() {
6062
// Only enable in debug mode. A backtrace uses ~1000 bytes of heap space and
@@ -334,12 +336,6 @@ function stringToFlags(flag) {
334336
return flag;
335337
}
336338

337-
// O_EXCL is mandated by POSIX, Windows supports it too.
338-
// Let's add a check anyway, just in case.
339-
if (!O_EXCL && ~flag.indexOf('x')) {
340-
throw errnoException('ENOSYS', 'fs.open(O_EXCL)');
341-
}
342-
343339
switch (flag) {
344340
case 'r' : return O_RDONLY;
345341
case 'rs' : return O_RDONLY | O_SYNC;
@@ -999,17 +995,6 @@ fs.appendFileSync = function(path, data, options) {
999995
fs.writeFileSync(path, data, options);
1000996
};
1001997

1002-
function errnoException(errorno, syscall) {
1003-
// TODO make this more compatible with ErrnoException from src/node.cc
1004-
// Once all of Node is using this function the ErrnoException from
1005-
// src/node.cc should be removed.
1006-
var e = new Error(syscall + ' ' + errorno);
1007-
e.errno = e.code = errorno;
1008-
e.syscall = syscall;
1009-
return e;
1010-
}
1011-
1012-
1013998
function FSWatcher() {
1014999
EventEmitter.call(this);
10151000

lib/net.js

+2-13
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ var timers = require('timers');
2525
var util = require('util');
2626
var assert = require('assert');
2727
var cares = process.binding('cares_wrap');
28+
2829
var cluster;
30+
var errnoException = util._errnoException;
2931

3032
function noop() {}
3133

@@ -940,19 +942,6 @@ function afterConnect(status, handle, req, readable, writable) {
940942
}
941943

942944

943-
function errnoException(errorno, syscall) {
944-
// TODO make this more compatible with ErrnoException from src/node.cc
945-
// Once all of Node is using this function the ErrnoException from
946-
// src/node.cc should be removed.
947-
var e = new Error(syscall + ' ' + errorno);
948-
e.errno = e.code = errorno;
949-
e.syscall = syscall;
950-
return e;
951-
}
952-
953-
954-
955-
956945
function Server(/* [ options, ] listener */) {
957946
if (!(this instanceof Server)) return new Server(arguments[0], arguments[1]);
958947
events.EventEmitter.call(this);

lib/tty.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ var TTY = process.binding('tty_wrap').TTY;
2626
var isTTY = process.binding('tty_wrap').isTTY;
2727
var util = require('util');
2828

29+
var errnoException = util._errnoException;
30+
31+
2932
exports.isatty = function(fd) {
3033
return isTTY(fd);
3134
};
@@ -123,12 +126,3 @@ WriteStream.prototype.clearScreenDown = function() {
123126
WriteStream.prototype.getWindowSize = function() {
124127
return [this.columns, this.rows];
125128
};
126-
127-
128-
// TODO share with net_uv and others
129-
function errnoException(errorno, syscall) {
130-
var e = new Error(syscall + ' ' + errorno);
131-
e.errno = e.code = errorno;
132-
e.syscall = syscall;
133-
return e;
134-
}

lib/util.js

+12
Original file line numberDiff line numberDiff line change
@@ -601,3 +601,15 @@ exports.pump = exports.deprecate(function(readStream, writeStream, callback) {
601601
call(err);
602602
});
603603
}, 'util.pump(): Use readableStream.pipe() instead');
604+
605+
606+
var uv;
607+
exports._errnoException = function(err, syscall) {
608+
if (typeof uv === 'undefined') uv = process.binding('uv');
609+
var errname = uv.errname(err);
610+
var e = new Error(syscall + ' ' + errname);
611+
e.code = errname;
612+
e.errno = errname;
613+
e.syscall = syscall;
614+
return e;
615+
};

src/node.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -459,16 +459,6 @@
459459
if (process._print_eval) console.log(result);
460460
}
461461

462-
function errnoException(errorno, syscall) {
463-
// TODO make this more compatible with ErrnoException from src/node.cc
464-
// Once all of Node is using this function the ErrnoException from
465-
// src/node.cc should be removed.
466-
var e = new Error(syscall + ' ' + errorno);
467-
e.errno = e.code = errorno;
468-
e.syscall = syscall;
469-
return e;
470-
}
471-
472462
function createWritableStdioStream(fd) {
473463
var stream;
474464
var tty_wrap = process.binding('tty_wrap');
@@ -651,6 +641,7 @@
651641
}
652642

653643
if (r) {
644+
var errnoException = NativeModule.require('util')._errnoException;
654645
throw errnoException(process._errno, 'kill');
655646
}
656647

@@ -685,6 +676,7 @@
685676
var r = wrap.start(signum);
686677
if (r) {
687678
wrap.close();
679+
var errnoException = NativeModule.require('util')._errnoException;
688680
throw errnoException(process._errno, 'uv_signal_start');
689681
}
690682

0 commit comments

Comments
 (0)