From bf72c7cb5de6cb7bde55ee08151e9e701eec89d9 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 23 Nov 2013 11:54:42 -0800 Subject: [PATCH] core: human friendly error for corrupt box metadata --- CHANGELOG.md | 1 + lib/vagrant/box.rb | 7 ++++++- lib/vagrant/errors.rb | 4 ++++ templates/locales/en.yml | 6 +++++- test/unit/vagrant/box_test.rb | 13 +++++++++++++ 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c72547cf..b4c4f47ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/vagrant/box.rb b/lib/vagrant/box.rb index 407d2f59b..e7e89a9ca 100644 --- a/lib/vagrant/box.rb +++ b/lib/vagrant/box.rb @@ -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 diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 6a8cd7466..835ef0ac3 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -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 diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 0b15212d5..4d42b3132 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -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. diff --git a/test/unit/vagrant/box_test.rb b/test/unit/vagrant/box_test.rb index 8176f19dd..93dbfea17 100644 --- a/test/unit/vagrant/box_test.rb +++ b/test/unit/vagrant/box_test.rb @@ -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