cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject git commit: CB-5782 Add a derived exception class for better error reporting in CLI
Date Tue, 21 Jan 2014 20:33:21 GMT
Updated Branches:
  refs/heads/master 4b9804391 -> 8aecd4966


CB-5782 Add a derived exception class for better error reporting in CLI

The on('uncaughtException') handler was removed recently in cordova-cli because
it was hiding the stack traces of some legit exceptions, but this results in
scary traces for simple errors like running outside a corodva project.

This change reintroduces the on('uncaughtException') handler and adds a special
CordovaError class for such simple errors. For exceptions of CordovaError class
only the message will be printed, for all other errors - the full stack trace.

Another pass over the code will be needed to find and convert the Errors to
CordovaErrors where appropriate. Will be done in a separate change.


Project: http://git-wip-us.apache.org/repos/asf/cordova-cli/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-cli/commit/8aecd496
Tree: http://git-wip-us.apache.org/repos/asf/cordova-cli/tree/8aecd496
Diff: http://git-wip-us.apache.org/repos/asf/cordova-cli/diff/8aecd496

Branch: refs/heads/master
Commit: 8aecd49667638b50a14709a8fa1021d0724675db
Parents: 4b98043
Author: Mark Koudritsky <kamrik@gmail.com>
Authored: Fri Jan 17 13:14:14 2014 -0500
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Tue Jan 21 15:33:13 2014 -0500

----------------------------------------------------------------------
 src/CordovaError.js | 31 +++++++++++++++++++++++++++++++
 src/cli.js          | 15 +++++++++++++--
 src/util.js         |  5 +++--
 3 files changed, 47 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/8aecd496/src/CordovaError.js
----------------------------------------------------------------------
diff --git a/src/CordovaError.js b/src/CordovaError.js
new file mode 100644
index 0000000..5576e06
--- /dev/null
+++ b/src/CordovaError.js
@@ -0,0 +1,31 @@
+/**
+    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.
+*/
+
+
+// A derived exception class. See usage example in cli.js
+// Based on:
+// stackoverflow.com/questions/1382107/whats-a-good-way-to-extend-error-in-javascript/8460753#8460753
+function CordovaError(message) {
+    Error.captureStackTrace(this, this.constructor);
+    this.name = this.constructor.name;
+    this.message = message;
+}
+CordovaError.prototype.__proto__ = Error.prototype;
+
+module.exports = CordovaError;

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/8aecd496/src/cli.js
----------------------------------------------------------------------
diff --git a/src/cli.js b/src/cli.js
index d22ba56..c45b556 100644
--- a/src/cli.js
+++ b/src/cli.js
@@ -18,6 +18,7 @@
 */
 
 var path = require('path'),
+    CordovaError = require('./CordovaError'),
     optimist, // required in try-catch below to print a nice error message if it's not installed.
     _;
 
@@ -63,6 +64,16 @@ module.exports = function CLI(inputArgs) {
             silent: args.silent
         };
 
+    // For CrodovaError print only the message without stack trace.
+    process.on('uncaughtException', function(err){
+        if (err instanceof CordovaError) {
+            console.error(err.message);
+        } else {
+            console.error(err.stack);
+        }
+        process.exit(1);
+    });
+
     cordova.on('results', console.log);
 
     if (!opts.silent) {
@@ -99,7 +110,7 @@ module.exports = function CLI(inputArgs) {
     }
 
     if (!cordova.hasOwnProperty(cmd)) {
-        throw new Error('Cordova does not know ' + cmd + '; try help for a list of all the
available commands.');
+        throw new CordovaError('Cordova does not know ' + cmd + '; try help for a list of
all the available commands.');
     }
 
     if (cmd === "info") {
@@ -128,7 +139,7 @@ module.exports = function CLI(inputArgs) {
         var customWww = args.src || args.link;
         if (customWww) {
             if (customWww.indexOf(':') != -1) {
-                throw new Error('Only local paths for custom www assets are supported.');
+                throw new CordovaError('Only local paths for custom www assets are supported.');
             }
             if (customWww.substr(0,1) === '~') {  // resolve tilde in a naive way.
                 customWww = path.join(process.env.HOME,  customWww.substr(1));

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/8aecd496/src/util.js
----------------------------------------------------------------------
diff --git a/src/util.js b/src/util.js
index 278172a..be843be 100644
--- a/src/util.js
+++ b/src/util.js
@@ -18,6 +18,7 @@
 */
 var fs            = require('fs'),
     path          = require('path'),
+    CordovaError  = require('./CordovaError'),
     shell         = require('shelljs');
 
 // Global configuration paths
@@ -75,11 +76,11 @@ exports = module.exports = {
         console.error('Hit an unhandled case in util.isCordova');
         return false;
     },
-    // Cd to project root dir and return its path. Throw if not in a Corodva project.
+    // Cd to project root dir and return its path. Throw CordovaError if not in a Corodva
project.
     cdProjectRoot: function() {
         var projectRoot = this.isCordova();
         if (!projectRoot) {
-            throw new Error('Current working directory is not a Cordova-based project.');
+            throw new CordovaError('Current working directory is not a Cordova-based project.');
         }
         process.chdir(projectRoot);
         return projectRoot;


Mime
View raw message