diff --git a/lib/vagrant/config/loader.rb b/lib/vagrant/config/loader.rb index 6dfa6cdc5..ffd66b2c9 100644 --- a/lib/vagrant/config/loader.rb +++ b/lib/vagrant/config/loader.rb @@ -194,13 +194,18 @@ module Vagrant def procs_for_path(path) @logger.debug("Load procs for pathname: #{path}") - begin - return Config.capture_configures do + return Config.capture_configures do + begin Kernel.load path + rescue SyntaxError => e + # Report syntax errors in a nice way. + raise Errors::VagrantfileSyntaxError, :file => e.message + rescue Exception => e + # Report the generic exception + raise Errors::VagrantfileLoadError, + :path => path, + :message => e.message end - rescue SyntaxError => e - # Report syntax errors in a nice way. - raise Errors::VagrantfileSyntaxError, :file => e.message end end end diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 8b9e8833a..e51d74a4f 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -428,6 +428,10 @@ module Vagrant error_key(:vagrantfile_exists) end + class VagrantfileLoadError < VagrantError + error_key(:vagrantfile_load_error) + end + class VagrantfileSyntaxError < VagrantError status_code(41) error_key(:vagrantfile_syntax_error) diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 5703ebbb0..27eb14616 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -278,6 +278,13 @@ en: vagrantfile_exists: |- `Vagrantfile` already exists in this directory. Remove it before running `vagrant init`. + vagrantfile_load_error: |- + There was an error loading a Vagrantfile. The file being loaded + and the error message are shown below. This is usually caused by + a syntax error. + + Path: %{path} + Message: %{message} vagrantfile_syntax_error: |- There is a syntax error in the following Vagrantfile. The syntax error message is reproduced below for convenience: diff --git a/test/unit/vagrant/config/loader_test.rb b/test/unit/vagrant/config/loader_test.rb index 9109eb863..309d5247b 100644 --- a/test/unit/vagrant/config/loader_test.rb +++ b/test/unit/vagrant/config/loader_test.rb @@ -175,5 +175,10 @@ describe Vagrant::Config::Loader do expect { instance.set(:file, temporary_file("Vagrant:^Config")) }. to raise_exception(Vagrant::Errors::VagrantfileSyntaxError) end + + it "should raise a proper error if there is a problem with the Vagrantfile" do + expect { instance.set(:file, temporary_file("foo")) }. + to raise_exception(Vagrant::Errors::VagrantfileLoadError) + end end end