Convert box actions to raise exceptions rather than error environment

This commit is contained in:
Mitchell Hashimoto 2010-08-28 12:31:55 -07:00
parent ccc45ebd7a
commit 3e1ccf0c4f
8 changed files with 28 additions and 31 deletions

View File

@ -24,7 +24,7 @@ module Vagrant
def call(env)
@env = env
return if !setup_box_directory
setup_box_directory
decompress
@app.call(@env)
@ -37,14 +37,10 @@ module Vagrant
end
def setup_box_directory
if File.directory?(@env["box"].directory)
@env.error!(:box_already_exists, :box_name => @env["box"].name)
return false
end
raise Errors::BoxAlreadyExists.new(:name => @env["box"].name) if File.directory?(@env["box"].directory)
FileUtils.mkdir_p(@env["box"].directory)
@box_directory = @env["box"].directory
true
end
def decompress

View File

@ -12,7 +12,7 @@ module Vagrant
env.ui.info "vagrant.actions.box.verify.verifying"
VirtualBox::Appliance.new(env["box"].ovf_file)
rescue Exception
return env.error!(:box_verification_failed)
raise Errors::BoxVerificationFailed.new
end
@app.call(env)

View File

@ -38,6 +38,11 @@ module Vagrant
error_key(:base_vm_not_found)
end
class BoxAlreadyExists < VagrantError
status_code(14)
error_key(:already_exists, "vagrant.actions.box.unpackage")
end
class BoxDownloadUnknownType < VagrantError
status_code(13)
error_key(:unknown_type, "vagrant.actions.box.download")
@ -48,6 +53,11 @@ module Vagrant
error_key(:box_not_found)
end
class BoxVerificationFailed < VagrantError
status_code(15)
error_key(:failed, "vagrant.actions.box.verify")
end
class CLIMissingEnvironment < VagrantError
status_code(1)
error_key(:cli_missing_env)

View File

@ -181,8 +181,17 @@ en:
unknown_type: "Unknown or unsupported URI type given for box download."
unpackage:
extracting: "Extracting box..."
already_exists: |-
A box already exists under the name of '%{name}'. This may or may
not be the same box you are trying to add. Please use another name
or remove the previous box then try to add it again.
verify:
verifying: "Verifying box..."
failed: |-
The box file you're attempting to add is invalid. This can be
commonly attributed to typos in the path given to the box add
command. Another common case of this is invalid packaging of the
box itself.
general:
package:

View File

@ -7,9 +7,6 @@
#---------------------------------------------------------------------
# CATEGORY: Error Messages
#---------------------------------------------------------------------
:box_already_exists: |-
This box appears to already exist! Please call `vagrant box remove <%= box_name %>`
and then try to add it again.
:box_download_http_socket_error: |-
An error occurred while trying to download the specified box. This most
often happens if there is no internet connection or the address is
@ -34,9 +31,6 @@
The box must be added through the `vagrant box add` command. Please view
the documentation associated with the command for more information.
:box_verification_failed: |-
The specified box is invalid. This can commonly be attributed to incorrectly
typing in the path to the box or invalid packaging of the box.
:box_not_specified: |-
No base box was specified! A base box is required as a staring point
for every vagrant virtual machine. Please specify one in your Vagrantfile

View File

@ -23,13 +23,6 @@ class UnpackageBoxActionTest < Test::Unit::TestCase
@app.expects(:call).with(@env)
@instance.call(@env)
end
should "halt the chain if setting up the box directory fails" do
@instance.expects(:setup_box_directory).returns(false)
@instance.expects(:decompress).never
@app.expects(:call).never
@instance.call(@env)
end
end
context "cleaning up" do
@ -59,9 +52,9 @@ class UnpackageBoxActionTest < Test::Unit::TestCase
should "error the environment if the box already exists" do
File.expects(:directory?).returns(true)
assert !@instance.setup_box_directory
assert @env.error?
assert_equal :box_already_exists, @env.error.first
assert_raises(Vagrant::Errors::BoxAlreadyExists) {
@instance.setup_box_directory
}
end
should "create the directory" do

View File

@ -27,9 +27,9 @@ class VerifyBoxActionTest < Test::Unit::TestCase
should "halt chain if verification fails" do
VirtualBox::Appliance.expects(:new).with(@env["box"].ovf_file).raises(Exception)
@app.expects(:call).with(@env).never
@instance.call(@env)
assert @env.error?
assert_equal :box_verification_failed, @env.error.first
assert_raises(Vagrant::Errors::BoxVerificationFailed) {
@instance.call(@env)
}
end
end
end

View File

@ -86,11 +86,6 @@ class ActionWardenTest < Test::Unit::TestCase
@instance.begin_rescue(new_env)
end
should "call error and exit" do
@instance.expects(:error_and_exit)
@instance.begin_rescue(new_env)
end
should "call exit if the environment is interupted" do
@instance.expects(:exit)
env = new_env