From 29e353bf8b612139d4807e0ed9f3d3b83be08a74 Mon Sep 17 00:00:00 2001 From: Patrick Mueller Date: Tue, 5 Jun 2012 08:59:39 -0400 Subject: [PATCH] [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. --- grunt.js | 75 +++++++++++++++++++++++++++++++++++ lib/common/utils.js | 20 +++++++--- lib/ios/plugin/ios/console.js | 7 +++- test/test.utils.js | 20 ++++++++++ 4 files changed, 115 insertions(+), 7 deletions(-) create mode 100644 grunt.js diff --git a/grunt.js b/grunt.js new file mode 100644 index 00000000..7617f2b3 --- /dev/null +++ b/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)} + ) +} diff --git a/lib/common/utils.js b/lib/common/utils.js index eecbae4a..8f1415ee 100644 --- a/lib/common/utils.js +++ b/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(); } diff --git a/lib/ios/plugin/ios/console.js b/lib/ios/plugin/ios/console.js index 4732ad1e..12472ea8 100644 --- a/lib/ios/plugin/ios/console.js +++ b/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(); } diff --git a/test/test.utils.js b/test/test.utils.js index 0b9fb69e..90c006b1 100644 --- a/test/test.utils.js +++ b/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]") + }) + }) });