core: nicer error if shorthand can't be found

This commit is contained in:
Mitchell Hashimoto 2014-01-23 22:48:15 -08:00
parent 1bd3275b3b
commit d08866e9f3
4 changed files with 40 additions and 1 deletions

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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|