summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2009-07-23 18:48:18 -0700
committerJames Turnbull <james@lovedthanlost.net>2009-07-31 16:41:41 +1000
commit832b6ff1e18cf403213cbeb42646b5740669e6a5 (patch)
tree4866da8e9748d75ca113c6a1a783ee3f8f763350
parent4ea3f17eee236bcbc4481b08eeb5ece1e51cc929 (diff)
downloadpuppet-832b6ff1e18cf403213cbeb42646b5740669e6a5.tar.gz
puppet-832b6ff1e18cf403213cbeb42646b5740669e6a5.tar.xz
puppet-832b6ff1e18cf403213cbeb42646b5740669e6a5.zip
Exiting from app failures instead of raising
This protects the user from seeing stack traces in normal situations. It makes sense here because this is explicitly for user interactions. Signed-off-by: Luke Kanies <luke@madstop.com>
-rw-r--r--lib/puppet/application.rb21
-rwxr-xr-xspec/unit/application.rb17
2 files changed, 26 insertions, 12 deletions
diff --git a/lib/puppet/application.rb b/lib/puppet/application.rb
index e7240f773..2882c8191 100644
--- a/lib/puppet/application.rb
+++ b/lib/puppet/application.rb
@@ -210,11 +210,11 @@ class Puppet::Application
# This is the main application entry point
def run
- run_preinit
- parse_options
- Puppet.settings.parse if should_parse_config?
- run_setup
- run_command
+ exit_on_fail("initialize") { run_preinit }
+ exit_on_fail("parse options") { parse_options }
+ exit_on_fail("parse configuration file") { Puppet.settings.parse } if should_parse_config?
+ exit_on_fail("prepare for execution") { run_setup }
+ exit_on_fail("run") { run_command }
end
def main
@@ -299,4 +299,15 @@ class Puppet::Application
end
end
+ private
+
+ def exit_on_fail(message, code = 1)
+ begin
+ yield
+ rescue RuntimeError, NotImplementedError => detail
+ puts detail.backtrace if Puppet[:trace]
+ $stderr.puts "Could not %s: %s" % [message, detail]
+ exit(code)
+ end
+ end
end
diff --git a/spec/unit/application.rb b/spec/unit/application.rb
index 5c3df0999..c087373ac 100755
--- a/spec/unit/application.rb
+++ b/spec/unit/application.rb
@@ -261,22 +261,25 @@ describe Puppet::Application do
@app.run
end
- it "should raise an error if no command can be called" do
- lambda { @app.run }.should raise_error(NotImplementedError)
+ it "should warn and exit if no command can be called" do
+ $stderr.expects(:puts)
+ @app.expects(:exit).with(1)
+ @app.run
end
it "should raise an error if dispatch returns no command" do
@app.stubs(:get_command).returns(nil)
-
- lambda { @app.run }.should raise_error(NotImplementedError)
+ $stderr.expects(:puts)
+ @app.expects(:exit).with(1)
+ @app.run
end
it "should raise an error if dispatch returns an invalid command" do
@app.stubs(:get_command).returns(:this_function_doesnt_exist)
-
- lambda { @app.run }.should raise_error(NotImplementedError)
+ $stderr.expects(:puts)
+ @app.expects(:exit).with(1)
+ @app.run
end
-
end
describe "when metaprogramming" do