Catch exceptions on download

This commit is contained in:
Mitchell Hashimoto 2010-07-08 21:52:56 -07:00
parent f558304b50
commit a0a6230455
5 changed files with 52 additions and 3 deletions

View File

@ -12,7 +12,8 @@ require File.expand_path("util/glob_loader", libdir)
# Load them up
Vagrant::GlobLoader.glob_require(libdir, %w{util/stacked_proc_runner
downloaders/base config provisioners/base provisioners/chef systems/base commands/base commands/box})
downloaders/base config provisioners/base provisioners/chef systems/base
commands/base commands/box action/exception_catcher})
# Initialize the built-in actions
Vagrant::Action.builtin!

View File

@ -5,6 +5,7 @@ module Vagrant
BASENAME = "box"
include Util
include ExceptionCatcher
attr_reader :temp_path
@ -19,9 +20,11 @@ module Vagrant
def call(env)
@env = env
return if !instantiate_downloader
download
catch_action_exception(env) do
download if instantiate_downloader
end
return if env.error?
@app.call(@env)
cleanup

View File

@ -0,0 +1,14 @@
module Vagrant
class Action
# A helper to catch any ActionExceptions raised and to
# apply the error to the environment.
module ExceptionCatcher
def catch_action_exception(env)
yield env
rescue ActionException => e
env.error!(e.key, e.data)
false
end
end
end
end

View File

@ -38,6 +38,7 @@ class DownloadBoxActionTest < Test::Unit::TestCase
should "halt the chain if downloader instantiation fails" do
seq = sequence("seq")
@env.error!(:foo)
@instance.expects(:instantiate_downloader).in_sequence(seq).returns(false)
@instance.expects(:download).never
@app.expects(:call).with(@env).never

View File

@ -0,0 +1,30 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class ExceptionCatcherTest < Test::Unit::TestCase
setup do
@klass = Class.new
@klass.send(:include, Vagrant::Action::ExceptionCatcher)
@env = Vagrant::Action::Environment.new(mock_environment)
@instance = @klass.new
end
should "run block and return result if no exception" do
result = @instance.catch_action_exception(@env) do
true
end
assert result
assert !@env.error?
end
should "run block and return false with error environment on exception" do
result = @instance.catch_action_exception(@env) do
raise Vagrant::Action::ActionException.new(:foo, :foo => :bar)
end
assert !result
assert @env.error?
assert_equal :foo, @env.error.first
end
end