From ce3506abccbd7999607297e6347b2060a8036534 Mon Sep 17 00:00:00 2001 From: Crag Wolfe Date: Tue, 4 Dec 2012 20:17:50 -0800 Subject: [PATCH] Catch all exceptions at the top level (but don't bury them!) rather than having dozens of begin/rescue blocks throughout the code. Also, don't print the trace to stdout, but let the user know where to find it (at /tmp/aeolus-cli-trace-#{Process.pid}.log). Add a cucumber test for the above. Set the environment variable THOR_COLUMNS in cucumber.yml so that output is always consistent for tests (and doesn't vary with the user's terminal width). Fixes issue https://github.com/aeolus-incubator/thor-cli/issues/6 --- bin/aeolus | 20 ++++++++++++++++++- cucumber.yml | 2 ++ features/exceptions.feature | 9 +++++++++ features/step_definitions/exceptions_steps.rb | 11 ++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 cucumber.yml create mode 100644 features/exceptions.feature create mode 100644 features/step_definitions/exceptions_steps.rb diff --git a/bin/aeolus b/bin/aeolus index e90e3c2..b8a6524 100755 --- a/bin/aeolus +++ b/bin/aeolus @@ -1,4 +1,22 @@ #!/usr/bin/env ruby require 'aeolus_cli/main' -AeolusCli::Main.start + +begin + AeolusCli::Main.start +rescue Exception => ex + puts "Error: #{ex.message}" + fname = "/tmp/aeolus-cli-trace-#{Process.pid}.log" + the_stack_trace = ex.backtrace().join($/) + begin + # it is very unlikely writing to /tmp would fail, but + # wrap this so we don't bury the original exception + f = File.open(fname, "w") + puts "For further debugging information, see #{fname}" + f.puts the_stack_trace + f.close + rescue + puts "Was unable to write to the following stack trace to #{fname}:" + puts the_stack_trace + end +end diff --git a/cucumber.yml b/cucumber.yml new file mode 100644 index 0000000..2ffd771 --- /dev/null +++ b/cucumber.yml @@ -0,0 +1,2 @@ +# Default terminal width we use for cucumber tests +default: THOR_COLUMNS=118 diff --git a/features/exceptions.feature b/features/exceptions.feature new file mode 100644 index 0000000..b0f8092 --- /dev/null +++ b/features/exceptions.feature @@ -0,0 +1,9 @@ +Feature: Exception Handling + + Scenario: Garbage URL Input + When I run `aeolus provider list --conductor-url=http://444.444.444.444:444/api --username=test --password=test` + Then the aeolus command should write the stack trace to file + And the output should contain: + """ + Error: + """ diff --git a/features/step_definitions/exceptions_steps.rb b/features/step_definitions/exceptions_steps.rb new file mode 100644 index 0000000..1be37f1 --- /dev/null +++ b/features/step_definitions/exceptions_steps.rb @@ -0,0 +1,11 @@ +require 'aruba/api' + +Then /^the aeolus command should write the stack trace to file$/ do + /For further debugging information, see (\/tmp\/aeolus-cli-trace-\d+.log)/ =~ + all_output + trace_filename = Regexp.last_match(1) + + # The last line in the stack trace file will look something like + # ....../thor-cli/bin/aeolus:6:in `
' + check_file_content(trace_filename, /bin\/aeolus:\d+:in `
/, true) +end