Skip to content

Commit

Permalink
fix: ensure debug mode doesn't crash with sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jul 13, 2018
1 parent cf1d72a commit fc76fe4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
16 changes: 15 additions & 1 deletion lib/drivers/node-mongodb-native/collection.js
Expand Up @@ -242,8 +242,17 @@ function format(obj, sub) {
for (var i = 0; i < numKeys; ++i) {
key = keys[i];
if (x[key]) {
let error;
if (typeof x[key].toBSON === 'function') {
x[key] = x[key].toBSON();
try {
// `session.toBSON()` throws an error. This means we throw errors
// in debug mode when using transactions, see gh-6712. As a
// workaround, catch `toBSON()` errors, try to serialize without
// `toBSON()`, and rethrow if serialization still fails.
x[key] = x[key].toBSON();
} catch (_error) {
error = _error;
}
}
if (x[key].constructor.name === 'Binary') {
x[key] = 'BinData(' + x[key].sub_type + ', "' +
Expand All @@ -260,6 +269,11 @@ function format(obj, sub) {
x[key] = {inspect: function() { return representation; }};
} else if (Array.isArray(x[key])) {
x[key] = x[key].map(map);
} else if (error != null) {
// If there was an error with `toBSON()` and the object wasn't
// already converted to a string representation, rethrow it.
// Open to better ideas on how to handle this.
throw error;
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions test/docs/transactions.test.js
Expand Up @@ -28,12 +28,13 @@ describe('transactions', function() {
});
})).
then(version => {
const sp = version.split('.');
if (parseInt(sp[0], 10) < 4) {
if (version[0] < 4) {
this.skip();
}
}).
catch(() => this.skip());
catch(() => {
this.skip()
});
});

it('basic example', function() {
Expand Down

0 comments on commit fc76fe4

Please sign in to comment.