core: human friendly error for corrupt box metadata

This commit is contained in:
Mitchell Hashimoto 2013-11-23 11:54:42 -08:00
parent 79d929a264
commit bf72c7cb5d
5 changed files with 29 additions and 2 deletions

View File

@ -22,6 +22,7 @@ BUG FIXES:
- core: Make sure machine IDs are always strings. [GH-2434]
- core: 100% CPU spike when waiting for SSH is fixed. [GH-2401]
- core: Command lookup works on systems where PATH is not valid UTF-8 [GH-2514]
- core: Human-friendly error if box metadata.json becomes corrupted. [GH-2305]
- guests/freebsd: Mounting NFS folders works. [GH-2400]
- guests/freebsd: Uses `sh` by default for shell. [GH-2485]
- guests/redhat: Down interface before messing up configuration file

View File

@ -47,7 +47,12 @@ module Vagrant
metadata_file = directory.join("metadata.json")
raise Errors::BoxMetadataFileNotFound, :name => @name if !metadata_file.file?
@metadata = JSON.parse(directory.join("metadata.json").read)
begin
@metadata = JSON.parse(directory.join("metadata.json").read)
rescue JSON::ParserError
raise Errors::BoxMetadataCorrupted, name: @name
end
@logger = Log4r::Logger.new("vagrant::box")
end

View File

@ -124,6 +124,10 @@ module Vagrant
error_key(:box_config_changing_box)
end
class BoxMetadataCorrupted < VagrantError
error_key(:box_metadata_corrupted)
end
class BoxMetadataFileNotFound < VagrantError
error_key(:box_metadata_file_not_found)
end

View File

@ -180,6 +180,10 @@ en:
a new box. This box, in turn, specified a different box. This isn't
allowed, as it could lead to infinite recursion of Vagrant configuration
loading. Please fix this.
box_metadata_corrupted: |-
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_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
@ -580,7 +584,7 @@ en:
Port: %{port}
Username: %{username}
Private key: %{key_path}
synced_folder_unusable: |
synced_folder_unusable: |-
The synced folder type '%{type}' is reporting as unusable for
your current setup. Please verify you have all the proper
prerequisites for using this shared folder type and try again.

View File

@ -40,6 +40,19 @@ describe Vagrant::Box do
instance.metadata.should == data
end
context "with a corrupt metadata file" do
before do
directory.join("metadata.json").open("w") do |f|
f.write("")
end
end
it "should raise an exception" do
expect { subject }.
to raise_error(Vagrant::Errors::BoxMetadataCorrupted)
end
end
context "without a metadata file" do
before :each do
directory.join("metadata.json").delete