Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to disable injecting context into error #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,18 @@ function requestHandler() {
}
```

## cls.createNamespace(name)
## cls.createNamespace(name, [options])

* return: {Namespace}

Each application wanting to use continuation-local values should create its own
namespace. Reading from (or, more significantly, writing to) namespaces that
don't belong to you is a faux pas.

By default the current context will be added to any thrown error running in a
namespace. If you wish to disable this behavior, set `options.injectErrorContext`
to `false` (defaults to `true`).

## cls.getNamespace(name)

* return: {Namespace}
Expand Down
25 changes: 16 additions & 9 deletions context.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ var ERROR_SYMBOL = 'error@context';
// load polyfill if native support is unavailable
if (!process.addAsyncListener) require('async-listener');

function Namespace(name) {
this.name = name;
function Namespace(name, injectErrorContext) {
this.name = name;
// changed in 2.7: no default context
this.active = null;
this._set = [];
this.id = null;
this.active = null;
this._set = [];
this.id = null;
this.injectErrorContext = injectErrorContext;
}

Namespace.prototype.set = function (key, value) {
Expand Down Expand Up @@ -49,7 +50,7 @@ Namespace.prototype.run = function (fn) {
return context;
}
catch (exception) {
if (exception) {
if (this.injectErrorContext && exception) {
exception[ERROR_SYMBOL] = context;
}
throw exception;
Expand All @@ -76,7 +77,7 @@ Namespace.prototype.bind = function (fn, context) {
return fn.apply(this, arguments);
}
catch (exception) {
if (exception) {
if (this.injectErrorContext && exception) {
exception[ERROR_SYMBOL] = context;
}
throw exception;
Expand Down Expand Up @@ -160,10 +161,16 @@ function get(name) {
return process.namespaces[name];
}

function create(name) {
function create(name, options) {
assert.ok(name, "namespace must be given a name!");

var namespace = new Namespace(name);
options = options || {};
// `== null` tests for `null` and `undefined`
if (options.injectErrorContext == null) {
options.injectErrorContext = true;
}

var namespace = new Namespace(name, options.injectErrorContext);
namespace.id = process.addAsyncListener({
create : function () { return namespace.active; },
before : function (context, storage) { if (storage) namespace.enter(storage); },
Expand Down
25 changes: 25 additions & 0 deletions test/error-handling.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,31 @@ test("synchronous throw checks if error exists", function (t) {
cls.destroyNamespace('cls@synchronous-null-error');
});

test("synchronous throw doesn't attach context", function (t) {
t.plan(2);

var namespace = cls.createNamespace('cls@synchronous-not-attached', {
injectErrorContext: false
});

namespace.run(function () {
namespace.set('value', 'transaction clear');
try {
namespace.run(function () {
namespace.set('value', 'transaction set');
throw new Error('cls@synchronous explosion');
});
}
catch (e) {
t.notOk(namespace.fromException(e), "context was not attached to error");
}

t.equal(namespace.get('value'), 'transaction clear', "everything was reset");
});

cls.destroyNamespace('cls@synchronous-not-attached');
});

test("throw in process.nextTick attaches the context", function (t) {
t.plan(3);

Expand Down
7 changes: 6 additions & 1 deletion test/namespaces.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var test = tap.test;
var context = require('../context.js');

test("namespace management", function (t) {
t.plan(8);
t.plan(10);

t.throws(function () { context.createNamespace(); }, "name is required");

Expand All @@ -26,4 +26,9 @@ test("namespace management", function (t) {
"destroying works");

t.notOk(process.namespaces.another, "namespace has been removed");

t.ok(namespace.injectErrorContext, "defaults to injecting error context");

namespace = context.createNamespace('explictOption', { injectErrorContext: false });
t.notOk(namespace.injectErrorContext, "setting injectErrorContext works");
});