Skip to content

Commit

Permalink
logging: throw when a circular reference is used in an entry (#1664)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus authored and callmehiphop committed Oct 5, 2016
1 parent 3e9df0c commit 10cd2ac
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/logging/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"google-gax": "^0.7.0",
"google-proto-files": "^0.8.0",
"is": "^3.0.1",
"is-circular": "^1.0.1",
"string-format-obj": "^1.0.0"
},
"devDependencies": {
Expand Down
5 changes: 5 additions & 0 deletions packages/logging/src/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
var common = require('@google-cloud/common');
var extend = require('extend');
var is = require('is');
var isCircular = require('is-circular');

/**
* Create an entry object to define new data to insert into a log.
Expand Down Expand Up @@ -122,6 +123,10 @@ Entry.fromApiResponse_ = function(entry) {
* @private
*/
Entry.prototype.toJSON = function() {
if (is.object(this.data) && isCircular([this.data])) {
throw new Error('The JSON data for this entry has a circular reference.');
}

var entry = extend(true, {}, this);

var whitelist = [
Expand Down
9 changes: 9 additions & 0 deletions packages/logging/test/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,15 @@ describe('Entry', function() {
assert.strictEqual(json.jsonPayload, converted);
});

it('should throw with a struct with a circular reference', function() {
entry.data = { val: true };
entry.data.data = entry.data;

assert.throws(function() {
entry.toJSON();
}, /The JSON data for this entry has a circular reference\./);
});

it('should assign string data as textPayload', function() {
entry.data = 'string';
var json = entry.toJSON();
Expand Down

0 comments on commit 10cd2ac

Please sign in to comment.