[command] Add informative error example that optionally shows backtraces.

This commit is contained in:
Eloy Durán
2014-01-17 17:36:34 +01:00
parent 2211375132
commit faec9527ca
3 changed files with 45 additions and 11 deletions

29
lib/motion/error.rb Normal file
View File

@@ -0,0 +1,29 @@
# Copyright (c) 2012, HipByte SPRL and contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
module Motion
# This class is extended, when using the commands, with the
# `CLAide::InformativeError` module. This will cause these errors to only
# show their message when raised, unless the `--verbose` option is specified.
class InformativeError < StandardError; end
end

View File

@@ -22,10 +22,19 @@
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
require 'motion/version'
require 'motion/error'
$:.unshift File.expand_path('../../../../vendor/CLAide/lib', __FILE__)
require 'claide'
module Motion
# This will cause these errors to only show their message when raised, unless
# the `--verbose` option is specified.
class InformativeError
include CLAide::InformativeError
end
end
module Motion; module Project
class Command < CLAide::Command
@@ -120,10 +129,6 @@ module Motion; module Project
#end
#end
class InformativeError < StandardError
include CLAide::InformativeError
end
protected
def die(message)

View File

@@ -21,6 +21,8 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
require 'motion/error'
require 'erb'
require 'fileutils'
@@ -58,19 +60,17 @@ module Motion; module Project
@template_directory = self.class.all_templates[@template_name]
unless @template_directory
$stderr.puts "Cannot find template `#{@template_name}' in #{Paths.join(' or ')}"
$stderr.puts "Available templates: " + self.class.all_templates.keys.join(', ')
exit 1
raise InformativeError, "Cannot find template `#{@template_name}' in " \
"#{Paths.join(' or ')}. Available templates: " \
"#{self.class.all_templates.keys.join(', ')}"
end
unless app_name.match(/^[\w\s-]+$/)
$stderr.puts "Invalid app name"
exit 1
raise InformativeError, "Invalid project name."
end
if File.exist?(app_name)
$stderr.puts "Directory `#{app_name}' already exists"
exit 1
raise InformativeError, "Directory `#{app_name}' already exists"
end
end