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

View File

@ -16,14 +16,14 @@ module Vagrant
def package_base
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)
end
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
raise VMNotCreatedError.new if !vm.created?
raise Errors::VMNotCreatedError.new if !vm.created?
package_vm(vm)
end

View File

@ -43,11 +43,11 @@ module Vagrant
def check_virtualbox!
version = VirtualBox.version
if version.nil?
error_and_exit(:virtualbox_not_detected)
raise Errors::VirtualBoxNotDetected.new
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")
error_and_exit(:virtualbox_invalid_ose, :version => version.to_s)
raise Errors::VirtualBoxInvalidOSE.new(:version => version.to_s)
end
end
end

View File

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

View File

@ -1,5 +1,9 @@
en:
vagrant:
#-------------------------------------------------------------------------------
# Translations for exception classes
#-------------------------------------------------------------------------------
errors:
base_vm_not_found: The base VM with the name '%{name}' was not 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_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.
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_not_found: A VM by the name of %{name} was not found.
#-------------------------------------------------------------------------------
# Translations for commands. e.g. `vagrant x`
#-------------------------------------------------------------------------------
commands:
box:
no_installed_boxes: There are no installed boxes! Use `vagrant box add` to add some.

View File

@ -226,19 +226,6 @@
:virtualbox_import_failure: |-
The VM import failed! Try running `VBoxManage import` on the box file
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: |-
Failed to connect to VM! Failed to boot?
:vm_base_not_found: |-

View File

@ -8,7 +8,7 @@ class CommandBaseTest < Test::Unit::TestCase
context "initialization" 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 }) }
end
end

View File

@ -8,7 +8,7 @@ class CommandGroupBaseTest < Test::Unit::TestCase
context "initialization" 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 }) }
end
end

View File

@ -14,7 +14,7 @@ class CommandHelpersTest < Test::Unit::TestCase
context "initializing environment" 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
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
@env.stubs(:root_path).returns(nil)
assert_raises(Vagrant::NoEnvironmentError) { command([], @env).require_environment }
assert_raises(Vagrant::Errors::NoEnvironmentError) { command([], @env).require_environment }
end
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
instance = command(["foo"], @env)
assert_raises(Vagrant::MultiVMEnvironmentRequired) {
assert_raises(Vagrant::Errors::MultiVMEnvironmentRequired) {
instance.target_vms
}
end
@ -89,7 +89,7 @@ class CommandHelpersTest < Test::Unit::TestCase
should "raise an exception if an invalid name is given" do
instance = command(["foo"], @env)
assert_raises(Vagrant::VMNotFoundError) {
assert_raises(Vagrant::Errors::VMNotFoundError) {
instance.target_vms
}
end

View File

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

View File

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