Permalink
Browse files

Continuing a thought: JS-level util.errnoException

I noticed new stubbed lib/ code similar to my needs; researched and
then started this change;

**two typos fixed**, stub consolidated and moved to util.js and expanded;
usage areas indicated; test file started
  • Loading branch information...
1 parent 389e2a0 commit d44177535032ea1a1ac81293de9cc00b2fe2846b @tshinnic committed Sep 12, 2011
Showing with 157 additions and 44 deletions.
  1. +2 −12 lib/child_process_uv.js
  2. +2 −10 lib/dgram_uv.js
  3. +1 −11 lib/dns_uv.js
  4. +1 −11 lib/net_uv.js
  5. +53 −0 lib/util.js
  6. +98 −0 test/simple/test-util-errnoexception.js
View
@@ -22,6 +22,7 @@
var EventEmitter = require('events').EventEmitter;
var Process = process.binding('process_wrap').Process;
var inherits = require('util').inherits;
+var errnoException = require('util').errnoException;
var constants; // if (!constants) constants = process.binding('constants');
@@ -303,7 +304,7 @@ ChildProcess.prototype.spawn = function(options) {
this._internal.close();
this._internal = null;
- throw errnoException("spawn", errno)
+ throw errnoException(errno, "spawn");
}
this.pid = this._internal.pid;
@@ -332,17 +333,6 @@ ChildProcess.prototype.spawn = function(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;
-}
-
-
ChildProcess.prototype.kill = function(sig) {
if (!constants) {
constants = process.binding('constants');
View
@@ -21,6 +21,7 @@
var util = require('util');
var events = require('events');
+var errnoException = util.errnoException;
var UDP = process.binding('udp_wrap').UDP;
@@ -300,19 +301,10 @@ function onMessage(handle, nread, buf, rinfo) {
var self = handle.socket;
if (nread == -1) {
- self.emit('error', errnoException('recvmsg'));
+ self.emit('error', errnoException(errno, 'recvmsg'));
}
else {
rinfo.size = buf.length; // compatibility
self.emit('message', buf, rinfo);
}
}
-
-
-// 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;
-}
View
@@ -22,17 +22,7 @@
var cares = process.binding('cares_wrap'),
net = require('net'),
isIp = net.isIP;
-
-
-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;
-}
+var errnoException = require('util').errnoException;
function familyToSym(family) {
View
@@ -3,6 +3,7 @@ var stream = require('stream');
var timers = require('timers');
var util = require('util');
var assert = require('assert');
+var errnoException = util.errnoException;
// constructor for lazy loading
function createPipe() {
@@ -550,17 +551,6 @@ function afterConnect(status, handle, req) {
}
-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 */) {
View
@@ -20,6 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.
var events = require('events');
+var constants; // if (!constants) constants = process.binding('constants');
var formatRegExp = /%[sdj%]/g;
@@ -534,3 +535,55 @@ exports.inherits = function(ctor, superCtor) {
}
});
};
+
+// Generate an syscall-like Error object with supplied errno, message, etc.
+// When arguments are supplied, error object filled members are:
+// e.errno usually number value for error
+// e.code can be name for error
+// e.syscall name of system call generating/related to error
+// e.message text describing error
+// e.path path associated with error
+
+function errnoException(errorno, syscall, message, path) {
+
+ var e = new Error();
+
+ var msg = '';
+ if (syscall) {
+ msg += syscall + ' ';
+ e.syscall = syscall;
+ }
+
+ if (errorno) {
+ if (!constants) {
+ constants = process.binding('constants');
+ }
+
+ // Try to resolve errno to both a name and a value
+ if (typeof errorno === 'string' && constants.hasOwnProperty(errorno)) {
+ e.code = errorno;
+ e.errno = constants[errorno];
+ msg += errorno;
+ } else {
+ //XXX reasonable to search for name corresponding to errno?
+ e.code = e.errno = errorno;
+ msg += errorno.toString();
+ }
+ }
+
+ if (message) {
+ if (msg !== '') { msg += ', '; }
+ msg += message;
+ }
+
+ if (path) {
+ if (msg !== '') { msg += ' '; }
+ msg += '\'' + path + '\'';
+ e.path = path;
+ }
+
+ e.message = msg;
+
+ return e;
+}
+exports.errnoException = errnoException;
@@ -0,0 +1,98 @@
+
+
+var common = require('../common');
+var assert = require('assert');
+var util = require('util');
+
+var e, ex;
+
+assert.equal(typeof util.errnoException, 'function',
+ 'errnoException API missing');
+
+e = util.errnoException();
+//console.log(' From empty args I see \n', e);
+
+assert.ok(e instanceof Error, 'return object is not an Error object');
+assert.equal(e.errno, undefined, 'e.errno set unexpectedly');
+assert.equal(e.code, undefined, 'e.code set unexpectedly');
+assert.equal(e.path, undefined, 'e.path set unexpectedly');
+assert.notEqual(e.message, undefined, 'e.message was not set');
+
+e = util.errnoException(2, 'foobar');
+//console.log(' From (2,foobar) args \n', e);
+
+assert.ok(e instanceof Error, 'return object is not an Error object');
+assert.equal(e.errno, 2, 'value 2 not set into e.errno');
+assert.equal(e.code, 2, 'value 2 not set into e.code');
+assert.equal(e.path, undefined, 'e.path set unexpectedly');
+assert.notEqual(e.message, undefined, 'e.message was not set');
+
+
+e = util.errnoException('ENOENT', 'foobar');
+//console.log(' From (ENOENT,foobar) args \n', e);
+
+assert.ok(e instanceof Error, 'return object is not an Error object');
+assert.equal(e.code, 'ENOENT', 'ENOENT not set into e.code');
+assert.equal(e.errno, 2, 'ENOENT not translated to number value 2');
+assert.equal(e.path, undefined, 'e.path set unexpectedly');
+assert.notEqual(e.message, undefined, 'e.message was not set');
+
+
+e = util.errnoException('ENOENT', 'foobar', 'baz');
+//console.log(' From (ENOENT,foobar,baz) args \n', e);
+
+assert.ok(e instanceof Error, 'return object is not an Error object');
+assert.equal(e.code, 'ENOENT', 'ENOENT not set into e.code');
+assert.equal(e.errno, 2, 'ENOENT not translated to number value 2');
+assert.equal(e.path, undefined, 'e.path set unexpectedly');
+assert.notEqual(e.message, undefined, 'e.message was not set');
+
+
+e = util.errnoException('ENOENT', 'foobar', 'baz', 'batcave');
+//console.log(' From (ENOENT,foobar,baz,batcave) args \n', e);
+
+assert.ok(e instanceof Error, 'return object is not an Error object');
+assert.equal(e.code, 'ENOENT', 'ENOENT not set into e.code');
+assert.equal(e.errno, 2, 'ENOENT not translated to number value 2');
+assert.equal(e.path, 'batcave', 'e.path not set from argument');
+assert.notEqual(e.message, undefined, 'e.message was not set');
+
+
+if(0){
+var net_binding = process.binding('net');
+var net_errnoException = net_binding.errnoException;
+
+console.log('\n');
+console.log(' net_errnoException is ', net_errnoException);
+console.log(' ', typeof net_errnoException);
+
+// { errno: 0, code: '', syscall: 'undefined' }
+e = net_errnoException();
+console.log(' From empty args I see ', util.inspect(e,true));
+console.log(' ', typeof e);
+console.log(' ', e instanceof Error);
+
+// { errno: 2, code: 'ENOENT', syscall: 'undefined' }
+e = net_errnoException(2);
+console.log(' From args (2) ', util.inspect(e,true));
+
+// { errno: 2, code: 'ENOENT', syscall: 'foobar' }
+e = net_errnoException(2, 'foobar');
+console.log(' From args (2,foobar) ', util.inspect(e,true));
+
+// { errno: 2, code: 'ENOENT', syscall: 'foobar' }
+e = net_errnoException(2, 'foobar', 'baz');
+console.log(' From args (2,foobar,bar) ', util.inspect(e,true));
+
+// { errno: 0, code: '', syscall: 'foobar' }
+e = net_errnoException('ENOENT', 'foobar');
+console.log(' From args (ENOENT,foobar)', util.inspect(e,true));
+
+}
+
+// EAGAIN 11
+// EWOULDBLOCK 11
+// ENOTSUP 95
+// EOPNOTSUPP 95
+
+

0 comments on commit d441775

Please sign in to comment.