incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pmue...@apache.org
Subject js commit: [CB-773] wrapper JSON.stringify calls in loggers
Date Tue, 05 Jun 2012 13:04:10 GMT
Updated Branches:
  refs/heads/master ea6b03d7b -> 29e353bf8


[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.


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/commit/29e353bf
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/29e353bf
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/29e353bf

Branch: refs/heads/master
Commit: 29e353bf8b612139d4807e0ed9f3d3b83be08a74
Parents: ea6b03d
Author: Patrick Mueller <pmuellr@apache.org>
Authored: Tue Jun 5 08:59:39 2012 -0400
Committer: Patrick Mueller <pmuellr@apache.org>
Committed: Tue Jun 5 08:59:39 2012 -0400

----------------------------------------------------------------------
 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(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/29e353bf/grunt.js
----------------------------------------------------------------------
diff --git a/grunt.js b/grunt.js
new file mode 100644
index 0000000..7617f2b
--- /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)}
+    )
+}

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/29e353bf/lib/common/utils.js
----------------------------------------------------------------------
diff --git a/lib/common/utils.js b/lib/common/utils.js
index eecbae4..8f1415e 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();
 }

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/29e353bf/lib/ios/plugin/ios/console.js
----------------------------------------------------------------------
diff --git a/lib/ios/plugin/ios/console.js b/lib/ios/plugin/ios/console.js
index 4732ad1..12472ea 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();
         }

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/29e353bf/test/test.utils.js
----------------------------------------------------------------------
diff --git a/test/test.utils.js b/test/test.utils.js
index 0b9fb69..90c006b 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]")
+        })
+
     })
     
 });


Mime
View raw message