Permalink
Browse files

[CB-773] wrapper JSON.stringify calls in loggers

Added try/catch wrappers around iOS's console's and
the logger's usage of JSON.stringify().

Added tests for logger; tested console in a Cordova app.

Drive by enhancement, added a grunt file which watches
the source files and runs grunt.  Replacement for "wr"
usage.
  • Loading branch information...
1 parent ea6b03d commit 29e353bf8b612139d4807e0ed9f3d3b83be08a74 @pmuellr pmuellr committed Jun 5, 2012
Showing with 115 additions and 7 deletions.
  1. +75 −0 grunt.js
  2. +14 −6 lib/common/utils.js
  3. +6 −1 lib/ios/plugin/ios/console.js
  4. +20 −0 test/test.utils.js
View
75 grunt.js
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+// open "http://search.npmjs.org/#/grunt" ; sudo npm -g install grunt
+
+var child_process = require("child_process")
+
+//------------------------------------------------------------------------------
+// list of source files to watch
+//------------------------------------------------------------------------------
+var sourceFiles = [
+ "build/**/*.js",
+ "grunt.js",
+ "Jakefile",
+ "lib/**/*.js",
+ "test/**/*.js"
+]
+
+//------------------------------------------------------------------------------
+var gruntConfig = {
+ watch: {
+ jake: {
+ files: sourceFiles,
+ tasks: ["jake"]
+ }
+ }
+}
+
+//------------------------------------------------------------------------------
+// run "jake"
+//------------------------------------------------------------------------------
+function jakeTask(grunt) {
+ var done = this.async()
+ var make = child_process.spawn('jake')
+
+ make.stdout.on("data", function(data) {
+ grunt.log.write("" + data)
+ })
+
+ make.stderr.on("data", function(data) {
+ grunt.log.error("" + data)
+ })
+
+ make.on("exit", function(code) {
+ if (code === 0) return done(true)
+
+ grunt.log.writeln("error running jake", code)
+ return done(false)
+ })
+}
+
+//------------------------------------------------------------------------------
+module.exports = function(grunt) {
+ grunt.initConfig(gruntConfig)
+
+ grunt.registerTask("default", "watch")
+ grunt.registerTask("jake", "run jake", function(){jakeTask.call(this,grunt)}
+ )
+}
View
20 lib/common/utils.js
@@ -121,9 +121,10 @@ utils.format = function(formatString /* ,... */) {
utils.vformat = function(formatString, args) {
if (formatString === null || formatString === undefined) return "";
if (arguments.length == 1) return formatString.toString();
+ if (typeof formatString != "string") return formatString.toString();
var pattern = /(.*?)%(.)(.*)/;
- var rest = formatString.toString();
+ var rest = formatString;
var result = [];
while (args.length) {
@@ -166,13 +167,20 @@ function UUIDcreatePart(length) {
//------------------------------------------------------------------------------
function formatted(object, formatChar) {
- switch(formatChar) {
- case 'j':
- case 'o': return JSON.stringify(object);
- case 'c': return '';
+ try {
+ switch(formatChar) {
+ case 'j':
+ case 'o': return JSON.stringify(object);
+ case 'c': return '';
+ }
+ }
+ catch (e) {
+ return "error JSON.stringify()ing argument: " + e;
}
- if (null === object) return Object.prototype.toString.call(object);
+ if ((object === null) || (object === undefined)) {
+ return Object.prototype.toString.call(object);
+ }
return object.toString();
}
View
7 lib/ios/plugin/ios/console.js
@@ -23,7 +23,12 @@ DebugConsole.prototype.setLevel = function(level) {
var stringify = function(message) {
try {
if (typeof message === "object" && JSON && JSON.stringify) {
- return JSON.stringify(message);
+ try {
+ return JSON.stringify(message);
+ }
+ catch (e) {
+ return "error JSON.stringify()ing argument: " + e;
+ }
} else {
return message.toString();
}
View
20 test/test.utils.js
@@ -187,6 +187,26 @@ describe("utils", function () {
it("handles ('1%x2%y3',4,5)", function() {
expect(utils.format('1%x2%y3',4,5)).toBe('14253')
})
+
+ var cycler
+
+ beforeEach(function(){
+ cycler = {a: 1}
+ cycler.cycler = cycler
+ })
+
+ it("handles cyclic objects as format string", function() {
+ expect(utils.format(cycler)).toBe("[object Object]")
+ })
+
+ it("handles cyclic objects as object arg", function() {
+ expect(utils.format("%o",cycler)).toMatch(/^error JSON\.stringify\(\)ing argument:/)
+ })
+
+ it("handles cyclic objects as string arg", function() {
+ expect(utils.format("%s",cycler)).toBe("[object Object]")
+ })
+
})
});

0 comments on commit 29e353b

Please sign in to comment.