Skip to content

Commit 8ce0a2c

Browse files
logging: throw when a circular reference is used in an entry
1 parent 009bc99 commit 8ce0a2c

3 files changed

Lines changed: 15 additions & 0 deletions

File tree

packages/logging/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"google-gax": "^0.7.0",
5959
"google-proto-files": "^0.8.0",
6060
"is": "^3.0.1",
61+
"is-circular": "^1.0.1",
6162
"string-format-obj": "^1.0.0"
6263
},
6364
"devDependencies": {

packages/logging/src/entry.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
var common = require('@google-cloud/common');
2424
var extend = require('extend');
2525
var is = require('is');
26+
var isCircular = require('is-circular');
2627

2728
/**
2829
* Create an entry object to define new data to insert into a log.
@@ -122,6 +123,10 @@ Entry.fromApiResponse_ = function(entry) {
122123
* @private
123124
*/
124125
Entry.prototype.toJSON = function() {
126+
if (is.object(this.data) && isCircular(this.data)) {
127+
throw new Error('The JSON data for this entry has a circular reference.');
128+
}
129+
125130
var entry = extend(true, {}, this);
126131

127132
var whitelist = [

packages/logging/test/entry.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ describe('Entry', function() {
186186
assert.strictEqual(json.jsonPayload, converted);
187187
});
188188

189+
it('should throw with a struct with a circular reference', function() {
190+
entry.data = { val: true };
191+
entry.data.data = entry.data;
192+
193+
assert.throws(function() {
194+
entry.toJSON();
195+
}, /The JSON data for this entry has a circular reference\./);
196+
});
197+
189198
it('should assign string data as textPayload', function() {
190199
entry.data = 'string';
191200
var json = entry.toJSON();

0 commit comments

Comments
 (0)