continuation-local-storage
Advanced tools
Comparing version 2.5.2 to 2.6.0
@@ -12,2 +12,3 @@ 'use strict'; | ||
var CONTEXTS_SYMBOL = 'cls@contexts'; | ||
var ERROR_SYMBOL = 'error@context'; | ||
@@ -47,2 +48,6 @@ // load polyfill if native support is unavailable | ||
} | ||
catch (exception) { | ||
exception[ERROR_SYMBOL] = context; | ||
throw exception; | ||
} | ||
finally { | ||
@@ -61,2 +66,6 @@ this.exit(context); | ||
} | ||
catch (exception) { | ||
exception[ERROR_SYMBOL] = context; | ||
throw exception; | ||
} | ||
finally { | ||
@@ -127,2 +136,12 @@ self.exit(context); | ||
/** | ||
* If an error comes out of a namespace, it will have a context attached to it. | ||
* This function knows how to find it. | ||
* | ||
* @param {Error} exception Possibly annotated error. | ||
*/ | ||
Namespace.prototype.fromException = function (exception) { | ||
return exception[ERROR_SYMBOL]; | ||
}; | ||
function get(name) { | ||
@@ -129,0 +148,0 @@ return namespaces[name]; |
{ | ||
"name": "continuation-local-storage", | ||
"version": "2.5.2", | ||
"version": "2.6.0", | ||
"description": "userland implementation of https://github.com/joyent/node/issues/5243", | ||
@@ -5,0 +5,0 @@ "main": "context.js", |
@@ -21,2 +21,3 @@ 'use strict'; | ||
cls.destroyNamespace('test'); | ||
t.end(); | ||
@@ -37,1 +38,74 @@ }); | ||
}); | ||
test("synchronous throw attaches the context", function (t) { | ||
t.plan(3); | ||
var namespace = cls.createNamespace('cls@synchronous'); | ||
namespace.set('value', 'transaction clear'); | ||
try { | ||
namespace.run(function () { | ||
namespace.set('value', 'transaction set'); | ||
throw new Error('cls@synchronous explosion'); | ||
}); | ||
} | ||
catch (e) { | ||
t.ok(namespace.fromException(e), "context was attached to error"); | ||
t.equal(namespace.fromException(e)['value'], 'transaction set', | ||
"found the inner value"); | ||
} | ||
t.equal(namespace.get('value'), 'transaction clear', "everything was reset"); | ||
cls.destroyNamespace('cls@synchronous'); | ||
}); | ||
test("throw in process.nextTick attaches the context", function (t) { | ||
t.plan(3); | ||
var namespace = cls.createNamespace('cls@nexttick'); | ||
var d = domain.create(); | ||
namespace.set('value', 'transaction clear'); | ||
d.on('error', function (e) { | ||
t.ok(namespace.fromException(e), "context was attached to error"); | ||
t.equal(namespace.fromException(e)['value'], 'transaction set', | ||
"found the inner value"); | ||
cls.destroyNamespace('cls@nexttick'); | ||
}); | ||
// tap is only trying to help | ||
process.nextTick(d.bind(function () { | ||
namespace.run(function () { | ||
namespace.set('value', 'transaction set'); | ||
throw new Error("cls@nexttick explosion"); | ||
}); | ||
})); | ||
t.equal(namespace.get('value'), 'transaction clear', "everything was reset"); | ||
}); | ||
test("throw in setTimeout attaches the context", function (t) { | ||
t.plan(3); | ||
var namespace = cls.createNamespace('cls@nexttick'); | ||
var d = domain.create(); | ||
namespace.set('value', 'transaction clear'); | ||
d.on('error', function (e) { | ||
t.ok(namespace.fromException(e), "context was attached to error"); | ||
t.equal(namespace.fromException(e)['value'], 'transaction set', | ||
"found the inner value"); | ||
cls.destroyNamespace('cls@nexttick'); | ||
}); | ||
// tap is only trying to help | ||
setTimeout(d.bind(function () { | ||
namespace.run(function () { | ||
namespace.set('value', 'transaction set'); | ||
throw new Error("cls@nexttick explosion"); | ||
}); | ||
})); | ||
t.equal(namespace.get('value'), 'transaction clear', "everything was reset"); | ||
}); |
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
82969
2007