Catch exceptions on download
This commit is contained in:
parent
f558304b50
commit
a0a6230455
|
@ -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!
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue