From a5b84f413e56943b7c8a292fb7733e4683dbf4b7 Mon Sep 17 00:00:00 2001 From: Teemu Matilainen Date: Fri, 20 Dec 2013 06:54:10 -0300 Subject: [PATCH] core: More informative error if plugin.json parsing fails --- lib/vagrant/errors.rb | 4 ++++ plugins/commands/plugin/state_file.rb | 8 +++++++- templates/locales/en.yml | 6 ++++++ .../unit/plugins/commands/plugin/state_file_test.rb | 13 +++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 598fb9a16..cfaad43c2 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -460,6 +460,10 @@ module Vagrant error_key(:plugin_not_installed) end + class PluginStateFileParseError < VagrantError + error_key(:plugin_state_file_not_parsable) + end + class SCPPermissionDenied < VagrantError error_key(:scp_permission_denied) end diff --git a/plugins/commands/plugin/state_file.rb b/plugins/commands/plugin/state_file.rb index 7846bf7b0..55603ffb8 100644 --- a/plugins/commands/plugin/state_file.rb +++ b/plugins/commands/plugin/state_file.rb @@ -10,7 +10,13 @@ module VagrantPlugins @data = {} if @path.exist? - @data = JSON.parse(@path.read) + begin + @data = JSON.parse(@path.read) + rescue JSON::ParserError => e + raise Vagrant::Errors::PluginStateFileParseError, + :path => path, :message => e.message + end + upgrade_v0! if !@data["version"] end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index fddfd91a4..0cb3212fb 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -501,6 +501,12 @@ en: prior to attempting to do anything with it. plugin_not_installed: |- The plugin '%{name}' is not installed. Please install it first. + plugin_state_file_not_parsable: |- + Failed to parse the state file "%{path}": + %{message} + + Please remove the file and reinstall the plugins. + If this error recurs, please report a bug. port_collision_resume: |- This VM cannot be resumed, because the forwarded ports would collide with a running program (it could be another virtual machine). Normally, diff --git a/test/unit/plugins/commands/plugin/state_file_test.rb b/test/unit/plugins/commands/plugin/state_file_test.rb index a29daa6c5..888c650b5 100644 --- a/test/unit/plugins/commands/plugin/state_file_test.rb +++ b/test/unit/plugins/commands/plugin/state_file_test.rb @@ -70,4 +70,17 @@ describe VagrantPlugins::CommandPlugin::StateFile do expect(plugins["foo"]["vagrant_version"]).to eql("0") end end + + context "with parse errors" do + before do + path.open("w+") do |f| + f.write("I'm not json") + end + end + + it "should raise a VagrantError" do + expect { subject }. + to raise_error(Vagrant::Errors::PluginStateFileParseError) + end + end end