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

View File

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

View File

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

View File

@ -181,8 +181,17 @@ en:
unknown_type: "Unknown or unsupported URI type given for box download." unknown_type: "Unknown or unsupported URI type given for box download."
unpackage: unpackage:
extracting: "Extracting box..." 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: verify:
verifying: "Verifying box..." 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: general:
package: package:

View File

@ -7,9 +7,6 @@
#--------------------------------------------------------------------- #---------------------------------------------------------------------
# CATEGORY: Error Messages # 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: |- :box_download_http_socket_error: |-
An error occurred while trying to download the specified box. This most An error occurred while trying to download the specified box. This most
often happens if there is no internet connection or the address is 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 box must be added through the `vagrant box add` command. Please view
the documentation associated with the command for more information. 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: |- :box_not_specified: |-
No base box was specified! A base box is required as a staring point 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 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) @app.expects(:call).with(@env)
@instance.call(@env) @instance.call(@env)
end 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 end
context "cleaning up" do context "cleaning up" do
@ -59,9 +52,9 @@ class UnpackageBoxActionTest < Test::Unit::TestCase
should "error the environment if the box already exists" do should "error the environment if the box already exists" do
File.expects(:directory?).returns(true) File.expects(:directory?).returns(true)
assert !@instance.setup_box_directory assert_raises(Vagrant::Errors::BoxAlreadyExists) {
assert @env.error? @instance.setup_box_directory
assert_equal :box_already_exists, @env.error.first }
end end
should "create the directory" do should "create the directory" do

View File

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

View File

@ -86,11 +86,6 @@ class ActionWardenTest < Test::Unit::TestCase
@instance.begin_rescue(new_env) @instance.begin_rescue(new_env)
end 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 should "call exit if the environment is interupted" do
@instance.expects(:exit) @instance.expects(:exit)
env = new_env env = new_env