Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Continuing a thought: JS-level util.errnoException #1689

Closed
wants to merge 1 commit into from

3 participants

@tshinnic

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

@tshinnic tshinnic 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
d441775
@tshinnic

I needed something that would create an Error object similar to what is
produced by a system error, e.g. ENOENT, where e.code, e.errno, and
e.message were filled in as would be expected.

I looked around and noticed these duplicate stubbed routines along with comments
indicating a wish for a JS-level equivalent for src/node.cc ErrnoException().

While seeing how the stubs were used I noticed two typos, as noted in change.

I've relocated a single copy of the stub code into lib/util.js. I don't
know if this is the appropriate place.

I've expanded the stub to handle 'message' and 'path' arguments.

I've also made a stab at resolving a string errno to the errno number.
There is a potential for confusion, but the convention seems to be
well-observed that err.errno is a number and err.code is the error
name string. I'm fudging by using lookup from constants.

The problem of resolving error names to numbers and vice-versa, along
with the problem/potential for supplying the correct default message
ala strerror() needs discussion. There is currently no nice way to
supply error names and descriptions to JS-level code.

@tjfontaine
Owner

perhaps superseded by #2695

@Nodejs-Jenkins

Can one of the admins verify this patch?

@tjfontaine
Owner

Most of this work was done in 0161ec8

@tjfontaine tjfontaine closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 12, 2011
  1. @tshinnic

    Continuing a thought: JS-level util.errnoException

    tshinnic authored
    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
This page is out of date. Refresh to see the latest.
View
14 lib/child_process_uv.js
@@ -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
12 lib/dgram_uv.js
@@ -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
12 lib/dns_uv.js
@@ -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
12 lib/net_uv.js
@@ -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
53 lib/util.js
@@ -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;
View
98 test/simple/test-util-errnoexception.js
@@ -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
+
+
Something went wrong with that request. Please try again.