From faec9527caa260db47f388f71114e5780839ad42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eloy=20Dur=C3=A1n?= Date: Fri, 17 Jan 2014 17:36:34 +0100 Subject: [PATCH] [command] Add informative error example that optionally shows backtraces. --- lib/motion/error.rb | 29 +++++++++++++++++++++++++++++ lib/motion/project/command.rb | 13 +++++++++---- lib/motion/project/template.rb | 14 +++++++------- 3 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 lib/motion/error.rb diff --git a/lib/motion/error.rb b/lib/motion/error.rb new file mode 100644 index 00000000..06627856 --- /dev/null +++ b/lib/motion/error.rb @@ -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 diff --git a/lib/motion/project/command.rb b/lib/motion/project/command.rb index 57f8ec33..05d7265a 100644 --- a/lib/motion/project/command.rb +++ b/lib/motion/project/command.rb @@ -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) diff --git a/lib/motion/project/template.rb b/lib/motion/project/template.rb index cf1afc7e..2e2e4621 100644 --- a/lib/motion/project/template.rb +++ b/lib/motion/project/template.rb @@ -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