Skip to content

Commit

Permalink
catching circular references when stringifying objects, fixes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Oct 28, 2014
1 parent 98e8e3e commit 4c62aa7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion bower.json
@@ -1,7 +1,7 @@
{
"name": "lazy-ass",
"main": "index.js",
"version": "0.5.2",
"version": "0.5.3",
"homepage": "https://github.com/bahmutov/lazy-ass",
"license": "MIT",
"ignore": [
Expand Down
8 changes: 7 additions & 1 deletion index.js
Expand Up @@ -31,7 +31,13 @@
if (arg instanceof Error) {
return total + arg.name + ' ' + arg.message;
}
return total + JSON.stringify(arg, null, 2);
var argString;
try {
argString = JSON.stringify(arg, null, 2);
} catch (err) {
argString = '[cannot stringify arg ' + k + ']';
}
return total + argString;
}, '');
return msg;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "lazy-ass",
"description": "Lazy assertions without performance penalty",
"version": "0.5.2",
"version": "0.5.3",
"author": "Gleb Bahmutov <gleb.bahmutov@gmail.com>",
"bugs": {
"url": "https://github.com/bahmutov/lazy-ass/issues"
Expand Down
16 changes: 16 additions & 0 deletions test/lazy-ass.spec.js
Expand Up @@ -164,6 +164,22 @@
});
});

describe('serializes circular objects', function () {
var foo = {
bar: 'bar'
};
foo.foo = foo;

it('can handle circular object', function () {
var msg = 'foo has circular reference';
expect(function () {
lazyAss(false, msg, foo);
}).to.throwException(function (err) {
expect(err.message).to.contain(msg);
});
});
});

describe('serialize arguments', function () {
function foo() {
la(false, arguments);
Expand Down

0 comments on commit 4c62aa7

Please sign in to comment.