core: Raise predictable error if box metadata downlaod fails

This commit is contained in:
Mitchell Hashimoto 2014-04-05 09:20:03 -07:00
parent 4d9717ae4c
commit 0e46c5d9de
4 changed files with 22 additions and 0 deletions

View File

@ -103,6 +103,9 @@ module Vagrant
opts = { headers: ["Accept: application/json"] }
Util::Downloader.new(url, tf.path, **opts).download!
BoxMetadata.new(File.open(tf.path, "r"))
rescue Errors::DownloaderError => e
raise Errors::BoxMetadataDownloadError,
message: e.extra_data[:message]
end
# Checks if the box has an update and returns the metadata, version,

View File

@ -168,6 +168,10 @@ module Vagrant
error_key(:box_metadata_corrupted)
end
class BoxMetadataDownloadError < VagrantError
error_key(:box_metadata_download_error)
end
class BoxMetadataFileNotFound < VagrantError
error_key(:box_metadata_file_not_found)
end

View File

@ -382,6 +382,11 @@ en:
The metadata associated with the box '%{name}' appears corrupted.
This is most often caused by a disk issue or system crash. Please
remove the box, re-add it, and try again.
box_metadata_download_error: |-
There was an error while downloading the metadata for this box.
The error message is shown below:
%{message}
box_metadata_file_not_found: |-
The "metadata.json" file for the box '%{name}' was not found.
Boxes require this file in order for Vagrant to determine the

View File

@ -219,6 +219,16 @@ describe Vagrant::Box do
expect(result.name).to eq("foo")
expect(result.description).to eq("bar")
end
it "raises an error if the download failed" do
dl = double("downloader")
Vagrant::Util::Downloader.stub(new: dl)
dl.should_receive(:download!).and_raise(
Vagrant::Errors::DownloaderError.new(message: "foo"))
expect { subject.load_metadata }.
to raise_error(Vagrant::Errors::BoxMetadataDownloadError)
end
end
describe "destroying" do