You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by st...@apache.org on 2014/01/31 00:16:53 UTC
[04/15] git commit: CB-5782 Add a derived exception class for better
error reporting in CLI
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/82c4f43d
Tree: http://git-wip-us.apache.org/repos/asf/cordova-cli/tree/82c4f43d
Diff: http://git-wip-us.apache.org/repos/asf/cordova-cli/diff/82c4f43d
Branch: refs/heads/master
Commit: 82c4f43d47552f787905ef1906fdb5c3908499b8
Parents: d029eaf
Author: Mark Koudritsky <ka...@gmail.com>
Authored: Fri Jan 17 13:14:14 2014 -0500
Committer: Steven Gill <st...@gmail.com>
Committed: Thu Jan 30 15:11:52 2014 -0800
----------------------------------------------------------------------
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/82c4f43d/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/82c4f43d/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/82c4f43d/src/util.js
----------------------------------------------------------------------
diff --git a/src/util.js b/src/util.js
index c93c264..a729507 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
@@ -79,11 +80,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;