Comparing version 0.2.0 to 0.2.1
@@ -45,3 +45,11 @@ | ||
exports._x = _x = function(cb, chk, func) { | ||
if(cb && typeof(cb) !== 'function') | ||
throw new Error('cb is not a function'); | ||
if(typeof(func) !== 'function') | ||
throw new Error('func is not a function'); | ||
var prev = new Error(/*'previous thread'*/); | ||
return function() { | ||
@@ -52,3 +60,4 @@ try { | ||
if(!cb) | ||
cb = a.length > 0 ? a[a.length - 1] : null; | ||
cb = a.length > 0 && typeof(a[a.length-1]) === 'function' ? | ||
a[a.length-1] : null; | ||
@@ -55,0 +64,0 @@ if(chk) |
@@ -6,3 +6,3 @@ { | ||
"keywords": [ "error", "handling", "asynchronous" ], | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"homepage": "http://github.com/ypocat/laeh2", | ||
@@ -9,0 +9,0 @@ "repository": { |
# Lightweight Asynchronous Error Handling v2 for Node.js (LAEH2) | ||
The previous version of LAEH[1] is now deprecated. | ||
> The previous version of LAEH[1] is now deprecated. | ||
The reason is that is that some functions were removed, namely the support for Express.js and the MongoDB utility. This is because it is possible to nicely support Express.js while maintaining only a single version of the callback wrapper function. The MongoDB support is something that should reside in the `ultiz` package. | ||
[1]: https://github.com/ypocat/laeh | ||
> The reason is that is that some functions were removed, namely the support for Express.js and the MongoDB utility. This is because it is possible to nicely support Express.js while maintaining only a single version of the callback wrapper function. The MongoDB support is something that should reside in the `ultiz` package. | ||
But the main reason is that the arguments of the `_x` function were swapped, which would silently break any LAEH1-dependent code. | ||
Changes since LAEH1: | ||
> Changes since LAEH1: | ||
* Only a single callback wrapper function, the `_x`. | ||
* The `cb` and `chk` were moved to the front of `_x`'s argument list, to make code more readable. | ||
* The `_x` function now nicely ties to error handling in Express.js and Connect. | ||
* Lean Stacks support was also updated to be even more terse, and formatting options were added to support utilities which parse stack traces based on newlines (e.g. the error template in Express.js). | ||
> * Only a single callback wrapper function, the `_x`. | ||
> * The `cb` and `chk` were moved to the front of `_x`'s argument list, to make code more readable. | ||
> * The `_x` function now nicely ties to error handling in Express.js and Connect. | ||
> * Lean Stacks support was also updated to be even more terse, and formatting options were added to support utilities which parse stack traces based on newlines (e.g. the error template in Express.js). | ||
@@ -24,3 +26,3 @@ | ||
someAsyncFunction(arg, arg, function(err, data) { | ||
asyncFunction(arg, arg, function(err, data) { | ||
// err is not checked but should be (a common case) | ||
@@ -38,3 +40,3 @@ throw new Error('fail'); // uncaught - will exit Node.js | ||
someAsyncFunctionWithCallback(arg, arg, function(err, data) { | ||
asyncFunction(arg, arg, function(err, data) { | ||
if(err) | ||
@@ -60,3 +62,3 @@ callback(err); | ||
someAsyncFunctionWithCallback(arg, arg, _x(callback, true, function(err, data) { | ||
asyncFunction(arg, arg, _x(callback, true, function(err, data) { | ||
throw new Error('fail'); | ||
@@ -67,3 +69,3 @@ })); | ||
Parameter explanation: | ||
Parameters for the `_x` LAEH2 wrapper function: | ||
@@ -144,2 +146,4 @@ * `callback`: in case of error return control to callback | ||
at Object.oncomplete (/Users/ypocat/Github/laeh2/lib/laeh2.js:56:9) | ||
(Notice that the parent stack trace is missing.) | ||
@@ -154,3 +158,15 @@ The `leanStacks(hiding, prettyMeta)` call is optional, the `hiding` will hide stack frames from Node's core .js files and from `laeh2.js` itself. The `prettyMeta` is the third parameter for the `JSON.stringify` function, which is used to serialize your metadata objects (see below), and leaving it empty or null will serialize your metadata objects in-line. | ||
./ex6.js(7 < 13) | ||
### Warning | ||
Don't use LAEH to wrap non-asynchronous callbacks, and especially non-asynchronous loop callbacks, as this can lead to nasty runtime errors. Consider e.g.: | ||
```js | ||
[ 'one', 'two', 'three' ].forEach(_x(cb, false, function(v) { | ||
throw new Error('unexpected'); | ||
})); | ||
``` | ||
This will call the `cb` callback three times (effectively forking your control flow), because the `Array.forEach()` will not stop looping when the callback is called. Correct approach here is to not wrap the synchronous callback in `_x`, and let the parent block (which should be protected by `_x`, by `try/catch`, or by its synchronous parent block) handle any exceptions. | ||
### Express.js | ||
@@ -179,2 +195,2 @@ | ||
In the `_x(cb, chk, func)`, the func is you callback to be wrapped. If it follows the node convention of `func(err, args)`, you can pass `chk` as true, which will automatically check for the `err` to be null, and call the eventual callback if it isn't null. The eventual callback is passed as the `cb` argument, or if omitted, it is tried to be derived from the last argument passed to the function you are wrapping, e.g. if the signature is `func(err, args, cb)`, the `cb` is taken from its arguments. | ||
In the `_x(cb, chk, func)`, the func is your callback to be wrapped. If it follows the node convention of `func(err, args)`, you can pass `chk` as true, which will automatically check for the `err` to be null, and call the eventual callback if it isn't null. The eventual callback is passed as the `cb` argument, or if omitted, it is tried to be derived from the last argument passed to the function you are wrapping, e.g. if the signature is `func(err, args, cb)`, the `cb` is taken from its arguments. |
15710
10
206
190
7