Scope errors into the Errors namespace. VirtualBox version errors converted to I18n

This commit is contained in:
Mitchell Hashimoto 2010-08-26 23:21:28 -07:00
parent 5c4293ae39
commit afab53bf9f
11 changed files with 110 additions and 83 deletions

View File

@ -4,13 +4,13 @@ module Vagrant
# Initializes the environment by pulling the environment out of # Initializes the environment by pulling the environment out of
# the configuration hash and sets up the UI if necessary. # the configuration hash and sets up the UI if necessary.
def initialize_environment(args, options, config) def initialize_environment(args, options, config)
raise CLIMissingEnvironment.new if !config[:env] raise Errors::CLIMissingEnvironment.new if !config[:env]
@env = config[:env] @env = config[:env]
@env.ui = UI::Shell.new(@env, shell) if !@env.ui.is_a?(UI::Shell) @env.ui = UI::Shell.new(@env, shell) if !@env.ui.is_a?(UI::Shell)
end end
def require_environment def require_environment
raise NoEnvironmentError.new if !env.root_path raise Errors::NoEnvironmentError.new if !env.root_path
end end
# This returns an array of {VM} objects depending on the arguments # This returns an array of {VM} objects depending on the arguments
@ -22,9 +22,9 @@ module Vagrant
if env.multivm? if env.multivm?
return env.vms.values if !self.name return env.vms.values if !self.name
vm = env.vms[self.name.to_sym] vm = env.vms[self.name.to_sym]
raise VMNotFoundError.new(:name => self.name) if !vm raise Errors::VMNotFoundError.new(:name => self.name) if !vm
else else
raise MultiVMEnvironmentRequired.new if self.name raise Errors::MultiVMEnvironmentRequired.new if self.name
vm = env.vms.values.first vm = env.vms.values.first
end end

View File

@ -16,14 +16,14 @@ module Vagrant
def package_base def package_base
vm = VM.find(options[:base], env) vm = VM.find(options[:base], env)
raise BaseVMNotFoundError.new(:name => options[:base]) if !vm.created? raise Errors::BaseVMNotFoundError.new(:name => options[:base]) if !vm.created?
package_vm(vm) package_vm(vm)
end end
def package_target def package_target
raise MultiVMTargetRequired.new(:command => "package") if target_vms.length > 1 raise Errors::MultiVMTargetRequired.new(:command => "package") if target_vms.length > 1
vm = target_vms.first vm = target_vms.first
raise VMNotCreatedError.new if !vm.created? raise Errors::VMNotCreatedError.new if !vm.created?
package_vm(vm) package_vm(vm)
end end

View File

@ -43,11 +43,11 @@ module Vagrant
def check_virtualbox! def check_virtualbox!
version = VirtualBox.version version = VirtualBox.version
if version.nil? if version.nil?
error_and_exit(:virtualbox_not_detected) raise Errors::VirtualBoxNotDetected.new
elsif version.to_f < 3.2 elsif version.to_f < 3.2
error_and_exit(:virtualbox_invalid_version, :version => version.to_s) raise Errors::VirtualBoxInvalidVersion.new(:version => version.to_s)
elsif version.to_s.downcase.include?("ose") elsif version.to_s.downcase.include?("ose")
error_and_exit(:virtualbox_invalid_ose, :version => version.to_s) raise Errors::VirtualBoxInvalidOSE.new(:version => version.to_s)
end end
end end
end end

View File

@ -1,61 +1,84 @@
module Vagrant module Vagrant
# Main superclass of any errors in Vagrant. This provides some module Errors
# convenience methods for setting the status code and error key. # Main superclass of any errors in Vagrant. This provides some
# The status code is used by the `vagrant` executable as the # convenience methods for setting the status code and error key.
# error code, and the error key is used as a default message from # The status code is used by the `vagrant` executable as the
# I18n. # error code, and the error key is used as a default message from
class VagrantError < StandardError # I18n.
def self.status_code(code = nil) class VagrantError < StandardError
define_method(:status_code) { code } def self.status_code(code = nil)
define_method(:status_code) { code }
end
def self.error_key(key=nil)
define_method(:error_key) { key }
end
def initialize(message=nil, *args)
message = translate_error(error_key, message) if respond_to?(:error_key)
super
end
protected
def translate_error(key, opts=nil)
I18n.t("vagrant.errors.#{key}", opts)
end
end end
def self.error_key(key=nil) class BaseVMNotFound < VagrantError
define_method(:error_key) { key } status_code(6)
error_key(:base_vm_not_found)
end end
def initialize(message=nil, *args) class BoxNotFound < VagrantError
message = I18n.t("vagrant.errors.#{error_key}", message) if respond_to?(:error_key) status_code(2)
super error_key(:box_not_found)
end end
end
class BaseVMNotFound < VagrantError class CLIMissingEnvironment < VagrantError
status_code(6) status_code(1)
error_key(:base_vm_not_found) error_key(:cli_missing_env)
end end
class BoxNotFound < VagrantError class MultiVMEnvironmentRequired < VagrantError
status_code(2) status_code(5)
error_key(:box_not_found) error_key(:multi_vm_required)
end end
class CLIMissingEnvironment < VagrantError class MultiVMTargetRequired < VagrantError
status_code(1) status_code(7)
error_key(:cli_missing_env) error_key(:multi_vm_target_required)
end end
class MultiVMEnvironmentRequired < VagrantError class NoEnvironmentError < VagrantError
status_code(5) status_code(3)
error_key(:multi_vm_required) error_key(:no_env)
end end
class MultiVMTargetRequired < VagrantError class VirtualBoxInvalidOSE < VagrantError
status_code(7) status_code(9)
error_key(:multi_vm_target_required) error_key(:virtualbox_invalid_ose)
end end
class NoEnvironmentError < VagrantError class VirtualBoxInvalidVersion < VagrantError
status_code(3) status_code(9)
error_key(:no_env) error_key(:virtualbox_invalid_version)
end end
class VMNotCreatedError < VagrantError class VirtualBoxNotDetected < VagrantError
status_code(6) status_code(8)
error_key(:vm_creation_required) error_key(:virtualbox_not_detected)
end end
class VMNotFoundError < VagrantError class VMNotCreatedError < VagrantError
status_code(4) status_code(6)
error_key(:vm_not_found) error_key(:vm_creation_required)
end
class VMNotFoundError < VagrantError
status_code(4)
error_key(:vm_not_found)
end
end end
end end

View File

@ -1,5 +1,9 @@
en: en:
vagrant: vagrant:
#-------------------------------------------------------------------------------
# Translations for exception classes
#-------------------------------------------------------------------------------
errors: errors:
base_vm_not_found: The base VM with the name '%{name}' was not found. base_vm_not_found: The base VM with the name '%{name}' was not found.
box_not_found: Box '%{name}' could not be found. box_not_found: Box '%{name}' could not be found.
@ -7,8 +11,25 @@ en:
multi_vm_required: A multi-vm environment is required for name specification to this command. multi_vm_required: A multi-vm environment is required for name specification to this command.
multi_vm_target_required: `vagrant %{command}` requires a specific VM name to target in a multi-VM environment. multi_vm_target_required: `vagrant %{command}` requires a specific VM name to target in a multi-VM environment.
no_env: No Vagrant environment detected. Run `vagrant init` to set one up. no_env: No Vagrant environment detected. Run `vagrant init` to set one up.
virtualbox_invalid_ose: |-
Vagrant has detected you're using an OSE ("Open Source Edition") of VirtualBox.
Vagrant currently doesn't support any of the OSE editions due to slight API
differences. Please download the regular package from virtualbox.org and install
to continue.
virtualbox_invalid_version: |-
Vagrant has detected that you have VirtualBox version %{version} installed!
Vagrant requires that you use at least VirtualBox version 3.2. Please install
a more recent version of VirtualBox to continue.
virtualbox_not_detected: |-
Vagrant could not detect VirtualBox! Make sure VirtualBox is properly installed.
If VirtualBox is installed, it may be an incorrect version. Vagrant currently
requires VirtualBox 3.2.x. Please install the proper version to continue.
vm_creation_required: VM must be created before running this command. Run `vagrant up` first. vm_creation_required: VM must be created before running this command. Run `vagrant up` first.
vm_not_found: A VM by the name of %{name} was not found. vm_not_found: A VM by the name of %{name} was not found.
#-------------------------------------------------------------------------------
# Translations for commands. e.g. `vagrant x`
#-------------------------------------------------------------------------------
commands: commands:
box: box:
no_installed_boxes: There are no installed boxes! Use `vagrant box add` to add some. no_installed_boxes: There are no installed boxes! Use `vagrant box add` to add some.

View File

@ -226,19 +226,6 @@
:virtualbox_import_failure: |- :virtualbox_import_failure: |-
The VM import failed! Try running `VBoxManage import` on the box file The VM import failed! Try running `VBoxManage import` on the box file
manually for more verbose error output. manually for more verbose error output.
:virtualbox_invalid_version: |-
Vagrant has detected that you have VirtualBox version <%= version %> installed!
Vagrant requires that you use at least VirtualBox version 3.2. Please install
a more recent version of VirtualBox to continue.
:virtualbox_not_detected: |-
Vagrant could not detect VirtualBox! Make sure VirtualBox is properly installed.
If VirtualBox is installed, it may be an incorrect version. Vagrant currently
requires VirtualBox 3.2.x. Please install the proper version to continue.
:virtualbox_invalid_ose: |-
Vagrant has detected you're using an OSE ("Open Source Edition") of VirtualBox.
Vagrant currently doesn't support any of the OSE editions due to slight API
differences. Please download the regular package from virtualbox.org and install
to continue.
:vm_failed_to_boot: |- :vm_failed_to_boot: |-
Failed to connect to VM! Failed to boot? Failed to connect to VM! Failed to boot?
:vm_base_not_found: |- :vm_base_not_found: |-

View File

@ -8,7 +8,7 @@ class CommandBaseTest < Test::Unit::TestCase
context "initialization" do context "initialization" do
should "require an environment" do should "require an environment" do
assert_raises(Vagrant::CLIMissingEnvironment) { @klass.new([], {}, {}) } assert_raises(Vagrant::Errors::CLIMissingEnvironment) { @klass.new([], {}, {}) }
assert_nothing_raised { @klass.new([], {}, { :env => @env }) } assert_nothing_raised { @klass.new([], {}, { :env => @env }) }
end end
end end

View File

@ -8,7 +8,7 @@ class CommandGroupBaseTest < Test::Unit::TestCase
context "initialization" do context "initialization" do
should "require an environment" do should "require an environment" do
assert_raises(Vagrant::CLIMissingEnvironment) { @klass.new([], {}, {}) } assert_raises(Vagrant::Errors::CLIMissingEnvironment) { @klass.new([], {}, {}) }
assert_nothing_raised { @klass.new([], {}, { :env => @env }) } assert_nothing_raised { @klass.new([], {}, { :env => @env }) }
end end
end end

View File

@ -14,7 +14,7 @@ class CommandHelpersTest < Test::Unit::TestCase
context "initializing environment" do context "initializing environment" do
should "raise an exception if no environment is given" do should "raise an exception if no environment is given" do
assert_raises(Vagrant::CLIMissingEnvironment) { command([], nil) } assert_raises(Vagrant::Errors::CLIMissingEnvironment) { command([], nil) }
end end
should "not raise an exception if environment is given and setup UI" do should "not raise an exception if environment is given and setup UI" do
@ -31,7 +31,7 @@ class CommandHelpersTest < Test::Unit::TestCase
should "raise an exception if no environment" do should "raise an exception if no environment" do
@env.stubs(:root_path).returns(nil) @env.stubs(:root_path).returns(nil)
assert_raises(Vagrant::NoEnvironmentError) { command([], @env).require_environment } assert_raises(Vagrant::Errors::NoEnvironmentError) { command([], @env).require_environment }
end end
should "not raise an exception if there is an environment" do should "not raise an exception if there is an environment" do
@ -59,7 +59,7 @@ class CommandHelpersTest < Test::Unit::TestCase
should "raise an exception if a name is specified" do should "raise an exception if a name is specified" do
instance = command(["foo"], @env) instance = command(["foo"], @env)
assert_raises(Vagrant::MultiVMEnvironmentRequired) { assert_raises(Vagrant::Errors::MultiVMEnvironmentRequired) {
instance.target_vms instance.target_vms
} }
end end
@ -89,7 +89,7 @@ class CommandHelpersTest < Test::Unit::TestCase
should "raise an exception if an invalid name is given" do should "raise an exception if an invalid name is given" do
instance = command(["foo"], @env) instance = command(["foo"], @env)
assert_raises(Vagrant::VMNotFoundError) { assert_raises(Vagrant::Errors::VMNotFoundError) {
instance.target_vms instance.target_vms
} }
end end

View File

@ -12,28 +12,24 @@ class EnvironmentTest < Test::Unit::TestCase
should "not error and exit if everything is good" do should "not error and exit if everything is good" do
VirtualBox.expects(:version).returns("3.2.4") VirtualBox.expects(:version).returns("3.2.4")
@klass.expects(:error_and_exit).never assert_nothing_raised { @klass.check_virtualbox! }
@klass.check_virtualbox!
end end
should "error and exit if VirtualBox is not installed or detected" do should "error and exit if VirtualBox is not installed or detected" do
@klass.expects(:error_and_exit).with(:virtualbox_not_detected).once
VirtualBox.expects(:version).returns(nil) VirtualBox.expects(:version).returns(nil)
@klass.check_virtualbox! assert_raises(Vagrant::Errors::VirtualBoxNotDetected) { @klass.check_virtualbox! }
end end
should "error and exit if VirtualBox is lower than version 3.2" do should "error and exit if VirtualBox is lower than version 3.2" do
version = "3.1.12r1041" version = "3.1.12r1041"
@klass.expects(:error_and_exit).with(:virtualbox_invalid_version, :version => version.to_s).once
VirtualBox.expects(:version).returns(version) VirtualBox.expects(:version).returns(version)
@klass.check_virtualbox! assert_raises(Vagrant::Errors::VirtualBoxInvalidVersion) { @klass.check_virtualbox! }
end end
should "error and exit for OSE VirtualBox" do should "error and exit for OSE VirtualBox" do
version = "3.2.6_OSE" version = "3.2.6_OSE"
@klass.expects(:error_and_exit).with(:virtualbox_invalid_ose, :version => version.to_s).once
VirtualBox.expects(:version).returns(version) VirtualBox.expects(:version).returns(version)
@klass.check_virtualbox! assert_raises(Vagrant::Errors::VirtualBoxInvalidOSE) { @klass.check_virtualbox! }
end end
end end

View File

@ -2,7 +2,7 @@ require "test_helper"
class ErrorsTest < Test::Unit::TestCase class ErrorsTest < Test::Unit::TestCase
setup do setup do
@super = Vagrant::VagrantError @super = Vagrant::Errors::VagrantError
end end
should "set the given status code" do should "set the given status code" do