Skip to content

Commit 4b5cfc4

Browse files
gergelykeothiym23
authored andcommitted
bind: extend exception only if exists
1 parent 30d58e5 commit 4b5cfc4

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

context.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ Namespace.prototype.run = function (fn) {
4949
return context;
5050
}
5151
catch (exception) {
52-
exception[ERROR_SYMBOL] = context;
52+
if (exception) {
53+
exception[ERROR_SYMBOL] = context;
54+
}
5355
throw exception;
5456
}
5557
finally {
@@ -74,7 +76,9 @@ Namespace.prototype.bind = function (fn, context) {
7476
return fn.apply(this, arguments);
7577
}
7678
catch (exception) {
77-
exception[ERROR_SYMBOL] = context;
79+
if (exception) {
80+
exception[ERROR_SYMBOL] = context;
81+
}
7882
throw exception;
7983
}
8084
finally {

test/error-handling.tap.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,29 @@ test("synchronous throw attaches the context", function (t) {
5959
cls.destroyNamespace('cls@synchronous');
6060
});
6161

62+
test("synchronous throw checks if error exists", function (t) {
63+
t.plan(2);
64+
65+
var namespace = cls.createNamespace('cls@synchronous-null-error');
66+
namespace.run(function () {
67+
namespace.set('value', 'transaction clear');
68+
try {
69+
namespace.run(function () {
70+
namespace.set('value', 'transaction set');
71+
throw null;
72+
});
73+
}
74+
catch (e) {
75+
// as we had a null error, cls couldn't set the new inner value
76+
t.equal(namespace.get('value'), 'transaction clear', 'from outer value');
77+
}
78+
79+
t.equal(namespace.get('value'), 'transaction clear', "everything was reset");
80+
});
81+
82+
cls.destroyNamespace('cls@synchronous-null-error');
83+
});
84+
6285
test("throw in process.nextTick attaches the context", function (t) {
6386
t.plan(3);
6487

0 commit comments

Comments
 (0)