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

Make error context a non-enumerable property to avoid serialization errors #82

Open
wants to merge 2 commits 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
__testfile
18 changes: 14 additions & 4 deletions context.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,13 @@ Namespace.prototype.run = function (fn) {
return context;
}
catch (exception) {
if (exception) {
exception[ERROR_SYMBOL] = context;
if (exception && !exception[ERROR_SYMBOL]) {
Object.defineProperty(exception, ERROR_SYMBOL, {
configurable: true,
enumerable: false,
writable: false,
value: context
});
}
throw exception;
}
Expand All @@ -76,8 +81,13 @@ Namespace.prototype.bind = function (fn, context) {
return fn.apply(this, arguments);
}
catch (exception) {
if (exception) {
exception[ERROR_SYMBOL] = context;
if (exception && !exception[ERROR_SYMBOL]) {
Object.defineProperty(exception, ERROR_SYMBOL, {
configurable: true,
enumerable: false,
writable: false,
value: context
});
}
throw exception;
}
Expand Down
32 changes: 29 additions & 3 deletions test/error-handling.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ test("throw in process.nextTick attaches the context", function (t) {
test("throw in setTimeout attaches the context", function (t) {
t.plan(3);

var namespace = cls.createNamespace('cls@nexttick');
var namespace = cls.createNamespace('cls@settimeout');
var d = domain.create();

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');
cls.destroyNamespace('cls@settimeout');
});

namespace.run(function () {
Expand All @@ -132,10 +132,36 @@ test("throw in setTimeout attaches the context", function (t) {
setTimeout(d.bind(function () {
namespace.run(function () {
namespace.set('value', 'transaction set');
throw new Error("cls@nexttick explosion");
throw new Error("cls@settimeout explosion");
});
}));

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

test("errors can be serialized", function (t) {
var namespace = cls.createNamespace('cls@serialized');
namespace.run(function () {
var d = domain.create();

d.on('error', function (blerg) {
var canSerialize = true;
try {
JSON.stringify(blerg);
} catch (e) {
canSerialize = false;
}
t.ok(canSerialize, "errors serialize safely");
cls.destroyNamespace('cls@serialized');
t.end();
});

// tap is only trying to help
process.nextTick(d.bind(function () {
namespace.run(function () {
throw new Error("explicitly nonlocal exit");
});
}));
});
});