Generate errors instead of hard-coding, with tests

This commit is contained in:
David Humphrey (:humph) david.humphrey@senecacollege.ca 2014-03-15 20:30:37 -04:00
parent fe7ec79f83
commit be9e0b50a8
2 changed files with 70 additions and 76 deletions

View File

@ -1,86 +1,82 @@
define(function(require) { define(function(require) {
var errors = {}; var errors = {};
var errno = -1;
/**
* DON'T CHANGE THE ORDER OF THIS ARRAY!!
* The errno property follows the order from -1 upward
*/
[ [
/** /**
* node.js errors * node.js errors
*/ */
'UNKNOWN:unknown error', '-1:UNKNOWN:unknown error',
'OK:success', '0:OK:success',
'EOF:end of file', '1:EOF:end of file',
'EADDRINFO:getaddrinfo error', '2:EADDRINFO:getaddrinfo error',
'EACCES:permission denied', '3:EACCES:permission denied',
'EAGAIN:resource temporarily unavailable', '4:EAGAIN:resource temporarily unavailable',
'EADDRINUSE:address already in use', '5:EADDRINUSE:address already in use',
'EADDRNOTAVAIL:address not available', '6:EADDRNOTAVAIL:address not available',
'EAFNOSUPPORT:address family not supported', '7:EAFNOSUPPORT:address family not supported',
'EALREADY:connection already in progress', '8:EALREADY:connection already in progress',
'EBADF:bad file descriptor', '9:EBADF:bad file descriptor',
'EBUSY:resource busy or locked', '10:EBUSY:resource busy or locked',
'ECONNABORTED:software caused connection abort', '11:ECONNABORTED:software caused connection abort',
'ECONNREFUSED:connection refused', '12:ECONNREFUSED:connection refused',
'ECONNRESET:connection reset by peer', '13:ECONNRESET:connection reset by peer',
'EDESTADDRREQ:destination address required', '14:EDESTADDRREQ:destination address required',
'EFAULT:bad address in system call argument', '15:EFAULT:bad address in system call argument',
'EHOSTUNREACH:host is unreachable', '16:EHOSTUNREACH:host is unreachable',
'EINTR:interrupted system call', '17:EINTR:interrupted system call',
'EINVAL:invalid argument', '18:EINVAL:invalid argument',
'EISCONN:socket is already connected', '19:EISCONN:socket is already connected',
'EMFILE:too many open files', '20:EMFILE:too many open files',
'EMSGSIZE:message too long', '21:EMSGSIZE:message too long',
'ENETDOWN:network is down', '22:ENETDOWN:network is down',
'ENETUNREACH:network is unreachable', '23:ENETUNREACH:network is unreachable',
'ENFILE:file table overflow', '24:ENFILE:file table overflow',
'ENOBUFS:no buffer space available', '25:ENOBUFS:no buffer space available',
'ENOMEM:not enough memory', '26:ENOMEM:not enough memory',
'ENOTDIR:not a directory', '27:ENOTDIR:not a directory',
'EISDIR:illegal operation on a directory', '28:EISDIR:illegal operation on a directory',
'ENONET:machine is not on the network', '29:ENONET:machine is not on the network',
'ENOTCONN:socket is not connected', // errno 30 skipped, as per https://github.com/rvagg/node-errno/blob/master/errno.js
'ENOTSOCK:socket operation on non-socket', '31:ENOTCONN:socket is not connected',
'ENOTSUP:operation not supported on socket', '32:ENOTSOCK:socket operation on non-socket',
'ENOENT:no such file or directory', '33:ENOTSUP:operation not supported on socket',
'ENOSYS:function not implemented', '34:ENOENT:no such file or directory',
'EPIPE:broken pipe', '35:ENOSYS:function not implemented',
'EPROTO:protocol error', '36:EPIPE:broken pipe',
'EPROTONOSUPPORT:protocol not supported', '37:EPROTO:protocol error',
'EPROTOTYPE:protocol wrong type for socket', '38:EPROTONOSUPPORT:protocol not supported',
'ETIMEDOUT:connection timed out', '39:EPROTOTYPE:protocol wrong type for socket',
'ECHARSET:invalid Unicode character', '40:ETIMEDOUT:connection timed out',
'EAIFAMNOSUPPORT:address family for hostname not supported', '41:ECHARSET:invalid Unicode character',
'EAISERVICE:servname not supported for ai_socktype', '42:EAIFAMNOSUPPORT:address family for hostname not supported',
'EAISOCKTYPE:ai_socktype not supported', // errno 43 skipped, as per https://github.com/rvagg/node-errno/blob/master/errno.js
'ESHUTDOWN:cannot send after transport endpoint shutdown', '44:EAISERVICE:servname not supported for ai_socktype',
'EEXIST:file already exists', '45:EAISOCKTYPE:ai_socktype not supported',
'ESRCH:no such process', '46:ESHUTDOWN:cannot send after transport endpoint shutdown',
'ENAMETOOLONG:name too long', '47:EEXIST:file already exists',
'EPERM:operation not permitted', '48:ESRCH:no such process',
'ELOOP:too many symbolic links encountered', '49:ENAMETOOLONG:name too long',
'EXDEV:cross-device link not permitted', '50:EPERM:operation not permitted',
'ENOTEMPTY:directory not empty', '51:ELOOP:too many symbolic links encountered',
'ENOSPC:no space left on device', '52:EXDEV:cross-device link not permitted',
'EIO:i/o error', '53:ENOTEMPTY:directory not empty',
'EROFS:read-only file system', '54:ENOSPC:no space left on device',
'ENODEV:no such device', '55:EIO:i/o error',
'ESPIPE:invalid seek', '56:EROFS:read-only file system',
'ECANCELED:operation canceled', '57:ENODEV:no such device',
'58:ESPIPE:invalid seek',
'59:ECANCELED:operation canceled',
/** /**
* Filer specific errors * Filer specific errors
*/ */
'ENOTMOUNTED:not mounted', '1000:ENOTMOUNTED:not mounted',
'EFILESYSTEMERROR:missing super node', '1001:EFILESYSTEMERROR:missing super node, use \'FORMAT\' flag to format filesystem.',
'ENOATTR:attribute does not exist' '1002:ENOATTR:attribute does not exist'
].forEach(function(e) { ].forEach(function(e) {
e = e.split(':'); e = e.split(':');
var err = e[0]; var errno = e[0],
var message = e[1]; err = e[1],
message = e[2];
function ctor(m) { function ctor(m) {
this.message = m || message; this.message = m || message;
@ -92,8 +88,6 @@ define(function(require) {
// We expose the error as both Errors.EINVAL and Errors[18] // We expose the error as both Errors.EINVAL and Errors[18]
errors[err] = errors[errno] = ctor; errors[err] = errors[errno] = ctor;
errno++;
}); });
return errors; return errors;

View File

@ -128,9 +128,9 @@ define(["Filer"], function(Filer) {
expect(Filer.Errors[57]).to.equal(Filer.Errors.ENODEV); expect(Filer.Errors[57]).to.equal(Filer.Errors.ENODEV);
expect(Filer.Errors[58]).to.equal(Filer.Errors.ESPIPE); expect(Filer.Errors[58]).to.equal(Filer.Errors.ESPIPE);
expect(Filer.Errors[59]).to.equal(Filer.Errors.ECANCELED); expect(Filer.Errors[59]).to.equal(Filer.Errors.ECANCELED);
expect(Filer.Errors[60]).to.equal(Filer.Errors.ENOTMOUNTED); expect(Filer.Errors[1000]).to.equal(Filer.Errors.ENOTMOUNTED);
expect(Filer.Errors[61]).to.equal(Filer.Errors.EFILESYSTEMERROR); expect(Filer.Errors[1001]).to.equal(Filer.Errors.EFILESYSTEMERROR);
expect(Filer.Errors[62]).to.equal(Filer.Errors.ENOATTR); expect(Filer.Errors[1002]).to.equal(Filer.Errors.ENOATTR);
}); });
}); });
}); });