abstract-object
Advanced tools
Comparing version 1.1.0 to 1.2.0
180
lib/Error.js
@@ -1,78 +0,132 @@ | ||
/* Copyright (c) 2013 Rod Vagg, MIT License */ | ||
var inherits = require('util').inherits | ||
(function() { | ||
var AbstractError, Err, NotImplementedError, createError, errors, firstLower, inherits, k, kCorruption, kIOError, kInvalidArgument, kInvalidFormat, kInvalidType, kNotFound, kNotOpened, kNotSupported, kOk, util; | ||
var kOk = 0 | ||
, kNotFound = 1 | ||
, kCorruption = 2 | ||
, kNotSupported = 3 | ||
, kInvalidArgument = 4 | ||
, kIOError = 5 | ||
, kNotOpened = 101 | ||
, kInvalidType = 102 | ||
, kInvalidFormat = 103 | ||
util = require("./util"); | ||
inherits = util.inherits; | ||
var errors = { | ||
"Ok": kOk | ||
, "NotFound": kNotFound | ||
, "Corruption": kCorruption | ||
, "NotSupported": kNotSupported | ||
, "InvalidArgument": kInvalidArgument | ||
, "IO": kIOError | ||
, "NotOpened": kNotOpened | ||
, "InvalidType": kInvalidType | ||
, "InvalidFormat": kInvalidFormat | ||
} | ||
firstLower = function(s) { | ||
return s[0].toLowerCase() + s.substring(1); | ||
}; | ||
function firstLower(s) { | ||
return s[0].toLowerCase()+s.substring(1) | ||
} | ||
module.exports.AbstractError = AbstractError = (function() { | ||
inherits(AbstractError, Error); | ||
function AbstractError(msg, errno) { | ||
Error.call(this, msg) | ||
this.code = errno | ||
this.message = msg | ||
if (Error.captureStackTrace) | ||
Error.captureStackTrace(this, arguments.callee) | ||
} | ||
function AbstractError(msg, errno) { | ||
Error.call(this, msg); | ||
this.code = errno; | ||
this.message = msg; | ||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this, arguments.callee); | ||
} | ||
} | ||
inherits(AbstractError, Error) | ||
return AbstractError; | ||
})(); | ||
function NotImplementedError() { | ||
AbstractError.call(this, "NotImplemented", kNotSupported) | ||
} | ||
module.exports.NotImplementedError = NotImplementedError = (function() { | ||
inherits(NotImplementedError, AbstractError); | ||
inherits(NotImplementedError, AbstractError) | ||
function NotImplementedError() { | ||
AbstractError.call(this, "NotImplemented", kNotSupported); | ||
} | ||
for (var k in errors) { | ||
AbstractError[k] = errors[k] | ||
//generate AbstractError.isNotFound(err) class methods: | ||
AbstractError["is"+k] = (function(i, aType) { | ||
return function(err) { | ||
return err.code === i || (err.code == null && err.message && err.message.substring(0, aType.length) === aType) | ||
} | ||
})(errors[k], k) | ||
//generate AbstractError.notFound() instance methods: | ||
AbstractError.prototype[firstLower(k)] = (function(aType) { | ||
return function() { | ||
return AbstractError[aType](this) | ||
} | ||
})("is"+k) | ||
if (errors[k]>0) { | ||
var Err = (function(i, aType){ | ||
return function(msg) { | ||
if (msg == null || msg == "") msg = aType | ||
AbstractError.call(this, msg, i) | ||
return NotImplementedError; | ||
})(); | ||
kOk = 0; | ||
kNotFound = 1; | ||
kCorruption = 2; | ||
kNotSupported = 3; | ||
kInvalidArgument = 4; | ||
kIOError = 5; | ||
kNotOpened = 101; | ||
kInvalidType = 102; | ||
kInvalidFormat = 103; | ||
errors = { | ||
Ok: kOk, | ||
NotFound: kNotFound, | ||
Corruption: kCorruption, | ||
NotSupported: kNotSupported, | ||
InvalidArgument: kInvalidArgument, | ||
IO: kIOError, | ||
NotOpened: kNotOpened, | ||
InvalidType: kInvalidType, | ||
InvalidFormat: kInvalidFormat | ||
}; | ||
module.exports.createError = createError = function(aType, aErrorCode) { | ||
var Err; | ||
AbstractError[aType] = aErrorCode; | ||
AbstractError["is" + aType] = (function(aErrorCode, aType) { | ||
return function(err) { | ||
return err.code === aErrorCode || ((err.code == null) && err.message && err.message.substring(0, aType.length) === aType); | ||
}; | ||
})(aErrorCode, aType); | ||
AbstractError.prototype[firstLower(aType)] = (function(aIsMethodName) { | ||
return function() { | ||
return AbstractError[aIsMethodName](this); | ||
}; | ||
})("is" + aType); | ||
return Err = (function() { | ||
inherits(Err, AbstractError); | ||
function Err(msg) { | ||
if ((msg == null) || msg === "") { | ||
msg = aType; | ||
} | ||
AbstractError.call(this, msg, aErrorCode); | ||
} | ||
})(errors[k], k) | ||
inherits(Err, AbstractError) | ||
//generate NotFoundError Classes | ||
module.exports[k+"Error"] = Err | ||
return Err; | ||
})(); | ||
}; | ||
for (k in errors) { | ||
Err = createError(k, errors[k]); | ||
if (errors[k] > 0) { | ||
module.exports[k + "Error"] = Err; | ||
} | ||
/* the error code | ||
AbstractError[k] = errors[k] | ||
#generate AbstractError.isNotFound(err) class methods: | ||
AbstractError["is" + k] = ((i, aType) -> | ||
(err) -> | ||
err.code is i or (not err.code? and err.message and err.message.substring(0, aType.length) is aType) | ||
)(errors[k], k) | ||
#generate AbstractError.notFound() instance methods: | ||
AbstractError::[firstLower(k)] = ((aType) -> | ||
-> | ||
AbstractError[aType] this | ||
)("is" + k) | ||
if errors[k] > 0 | ||
Err = ((i, aType) -> | ||
(msg) -> | ||
msg = aType if not msg? or msg is "" | ||
AbstractError.call this, msg, i | ||
)(errors[k], k) | ||
inherits Err, AbstractError | ||
#generate NotFoundError,... Classes | ||
module.exports[k + "Error"] = Err | ||
*/ | ||
} | ||
} | ||
module.exports.AbstractError = AbstractError | ||
module.exports.NotImplementedError = NotImplementedError | ||
}).call(this); | ||
//# sourceMappingURL=Error.js.map |
{ | ||
"name": "abstract-object", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "AbstractObject with Object State Events Support, RefObject with RefCount and AddRef/Release Support.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/snowyu/abstract-object", |
@@ -102,5 +102,85 @@ # AbtractObject | ||
# AbstractError Classes | ||
## AbstractError | ||
All Errors are derived from the AbstractError. | ||
* Members: | ||
* message: the error message. | ||
* code: the error code. | ||
* Methods: | ||
* ok() | ||
* notFound() | ||
* .... | ||
* invalidFormat() | ||
* Class Methods: | ||
* AbstractError.isOk(err) | ||
* AbstractError.isNotFound(err) | ||
* ... | ||
the error codes: | ||
* AbstractError.Ok = 0 | ||
* AbstractError.NotFound = 1 | ||
* AbstractError.Corruption = 2 | ||
* AbstractError.NotSupported = 3 | ||
* AbstractError.InvalidArgument = 4 | ||
* AbstractError.IO = 5 | ||
* AbstractError.NotOpened = 6 | ||
* AbstractError.InvalidType = 7 | ||
* AbstractError.InvalidFormat = 8 | ||
## Other Error Classes: | ||
* NotFoundError | ||
* CorruptionError | ||
* NotSupportedError/NotImplementedError | ||
* InvalidArgumentError | ||
* IOError | ||
* NotOpenedError | ||
* InvalidTypeError | ||
* InvalidFormatError | ||
## Extends the AbstractError | ||
use the `createError` function can extend the AbstractError. | ||
createError(typeName, errorCode) | ||
__arguments__ | ||
* typeName *(string)*: the error type name, the first character must be upper case. | ||
* errorCode: *(number)*: the error code, it should be greater than 1000. | ||
__return__ | ||
* the error class | ||
### Usage | ||
```js | ||
var Errors = require("abstract-object/Error") | ||
var AbstractError = Errors.AbstractError | ||
var createError = Errors.createError | ||
var AlreadyReadError = createError('AlreadyRead', 10000) | ||
var err = new AlreadyReadError("already read over error.") | ||
assert.ok(AbstractError.isAlreadyRead(err)) | ||
assert.ok(AlreadyReadError.isAlreadyRead(err)) | ||
assert.ok(err.alreadyRead()) | ||
assert.equal(err.message, "already read over error.") | ||
assert.equal(err.code, 10000) | ||
``` | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37468
413
186