diff --git a/lib/vagrant/action/builtin/box_add.rb b/lib/vagrant/action/builtin/box_add.rb index a1e71f18c..407e1a8e2 100644 --- a/lib/vagrant/action/builtin/box_add.rb +++ b/lib/vagrant/action/builtin/box_add.rb @@ -26,12 +26,25 @@ module Vagrant # If we received a shorthand URL ("mitchellh/precise64"), # then expand it properly. + expanded = false uri = URI.parse(url) if !uri.scheme && !File.file?(url) + expanded = true url = "#{Vagrant.server_url}/#{url}" end - if metadata_url?(url, env) + is_metadata = false + begin + is_metadata = metadata_url?(url, env) + rescue Errors::DownloaderError => e + raise if !expanded + raise Errors::BoxAddShortNotFound, + error: e.extra_data[:message], + name: env[:box_url], + url: url + end + + if is_metadata add_from_metadata(url, env) else add_direct(url, env) diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 1984c5efd..4474854b8 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -132,6 +132,10 @@ module Vagrant error_key(:box_add_no_matching_version) end + class BoxAddShortNotFound < VagrantError + error_key(:box_add_short_not_found) + end + class BoxAlreadyExists < VagrantError error_key(:box_add_exists) end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 8528460e9..d68beeb2d 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -247,6 +247,13 @@ en: Address: %{url} Constraints: %{constraints} Available versions: %{versions} + box_add_short_not_found: |- + The box '%{name}' could not be found or could not be accessed + in the remote catalog. Please double-check the name. The + expanded URL and error message are shown below. + + URL: %{url} + Error: %{error} boot_bad_state: |- The guest machine entered an invalid state while waiting for it to boot. Valid states are '%{valid}'. The machine is in the diff --git a/test/unit/vagrant/action/builtin/box_add_test.rb b/test/unit/vagrant/action/builtin/box_add_test.rb index def9a1d4e..ea9e8484f 100644 --- a/test/unit/vagrant/action/builtin/box_add_test.rb +++ b/test/unit/vagrant/action/builtin/box_add_test.rb @@ -234,6 +234,21 @@ describe Vagrant::Action::Builtin::BoxAdd do end end + it "raises an error if shorthand is invalid" do + tf = Tempfile.new("foo") + tf.close + + with_web_server(Pathname.new(tf.path)) do |port| + env[:box_url] = "mitchellh/precise64.json" + + box_collection.should_receive(:add).never + app.should_receive(:call).never + + expect { subject.call(env) }. + to raise_error(Vagrant::Errors::BoxAddShortNotFound) + end + end + it "adds the latest version of a box with only one provider" do box_path = iso_env.box2_file(:virtualbox) tf = Tempfile.new("vagrant").tap do |f|