Skip to content

Commit

Permalink
more explicit error for failed actions
Browse files Browse the repository at this point in the history
  • Loading branch information
cldwalker committed Jul 13, 2009
1 parent 3828c6e commit 01c4e91
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/bond/agent.rb
Expand Up @@ -16,6 +16,8 @@ def complete(options={}, &block)

def call(input)
(mission = find_mission(input)) ? mission.execute : default_mission.execute(input)
rescue FailedExecutionError
$stderr.puts "", $!.message
rescue
p $!
p $!.backtrace.slice(0,5)
Expand Down
7 changes: 6 additions & 1 deletion lib/bond/mission.rb
@@ -1,5 +1,6 @@
module Bond
class InvalidMissionError < StandardError; end
class FailedExecutionError < StandardError; end
class Mission
attr_reader :action, :default

Expand Down Expand Up @@ -49,11 +50,15 @@ def matches?(input)
def execute(*args)
if args.empty?
list = @action.call(@input)
list = @search ? @search.call(@input, list) : list
list = (@search ? @search.call(@input, list) : list) || []
@list_prefix ? list.map {|e| @list_prefix + e } : list
else
@action.call(*args)
end
rescue
error_message = "Mission action failed to execute properly. Check your mission action for pattern #{@condition.inspect}.\n" +
"Failed with error: #{$!.message}"
raise FailedExecutionError, error_message
end

def default_search(input, list)
Expand Down
7 changes: 6 additions & 1 deletion test/agent_test.rb
Expand Up @@ -32,12 +32,17 @@ class Bond::AgentTest < Test::Unit::TestCase
complete 'blah'
end

test "chooses default mission if mission processing fails" do
test "chooses default mission if internal processing fails" do
Bond.complete(:on=>/bling/) {|e| [] }
Bond.agent.expects(:find_mission).raises
Bond.agent.default_mission.expects(:execute)
complete('bling')
end

test "prints error if action generates failure" do
Bond.complete(:on=>/bling/) {|e| raise "whoops" }
capture_stderr { complete('bling') }.should =~ /bling.*whoops/m
end
end

test "default_mission set to a valid mission if irb doesn't exist" do
Expand Down
2 changes: 1 addition & 1 deletion test/mission_test.rb
Expand Up @@ -17,7 +17,7 @@ class Bond::MissionTest < Test::Unit::TestCase
complete('cool c', 'c').should == %w{cd}
end

test "with quoted method completes" do
test "with method and quoted argument completes" do
Bond.complete(:on=>/bling/) {|e| [] }
Bond.complete(:method=>'cool') {|e| %w{ab cd ef ad} }
complete('cool "a', 'a').should == %w{ab ad}
Expand Down

0 comments on commit 01c4e91

Please sign in to comment.