Skip to content

Commit

Permalink
handle JSON exception
Browse files Browse the repository at this point in the history
avoid “TypeError: Converting circular structure to JSON"
  • Loading branch information
pbakondy committed Jun 29, 2015
1 parent f3c4b13 commit 8b1f2ca
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "filelogger",
"version": "1.1.0",
"version": "1.1.1",
"homepage": "https://github.com/pbakondy/filelogger",
"authors": [
"Peter Bakondy <[email protected]>"
Expand Down
15 changes: 13 additions & 2 deletions dist/filelogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,24 @@ angular.module('fileLogger', ['ngCordova.plugins.file'])

var messages = Array.prototype.slice.call(arguments, 1);
var message = [ timestamp, level ];
var text;

for (var i = 0; i < messages.length; i++ ) {
if (angular.isArray(messages[i])) {
message.push(JSON.stringify(messages[i]));
text = '[Array]';
try {
// avoid "TypeError: Converting circular structure to JSON"
text = JSON.stringify(messages[i]);
} catch(e) {}
message.push(text);
}
else if (angular.isObject(messages[i])) {
message.push(JSON.stringify(messages[i]));
text = '[Object]';
try {
// avoid "TypeError: Converting circular structure to JSON"
text = JSON.stringify(messages[i]);
} catch(e) {}
message.push(text);
}
else {
message.push(messages[i]);
Expand Down
2 changes: 1 addition & 1 deletion dist/filelogger.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "filelogger",
"private": false,
"main": "dist/filelogger",
"version": "1.1.0",
"version": "1.1.1",
"repository": {
"url": "git://github.com/pbakondy/filelogger.git"
},
Expand Down
15 changes: 13 additions & 2 deletions src/filelogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,24 @@ angular.module('fileLogger', ['ngCordova.plugins.file'])

var messages = Array.prototype.slice.call(arguments, 1);
var message = [ timestamp, level ];
var text;

for (var i = 0; i < messages.length; i++ ) {
if (angular.isArray(messages[i])) {
message.push(JSON.stringify(messages[i]));
text = '[Array]';
try {
// avoid "TypeError: Converting circular structure to JSON"
text = JSON.stringify(messages[i]);
} catch(e) {}
message.push(text);
}
else if (angular.isObject(messages[i])) {
message.push(JSON.stringify(messages[i]));
text = '[Object]';
try {
// avoid "TypeError: Converting circular structure to JSON"
text = JSON.stringify(messages[i]);
} catch(e) {}
message.push(text);
}
else {
message.push(messages[i]);
Expand Down

0 comments on commit 8b1f2ca

Please sign in to comment.