Give a nice human-friendly error message when problems loading Vfile

This commit is contained in:
Mitchell Hashimoto 2013-01-30 20:01:41 -08:00
parent 72c10b53e7
commit c57ba9de58
4 changed files with 26 additions and 5 deletions

View File

@ -194,13 +194,18 @@ module Vagrant
def procs_for_path(path) def procs_for_path(path)
@logger.debug("Load procs for pathname: #{path}") @logger.debug("Load procs for pathname: #{path}")
begin
return Config.capture_configures do return Config.capture_configures do
begin
Kernel.load path Kernel.load path
end
rescue SyntaxError => e rescue SyntaxError => e
# Report syntax errors in a nice way. # Report syntax errors in a nice way.
raise Errors::VagrantfileSyntaxError, :file => e.message raise Errors::VagrantfileSyntaxError, :file => e.message
rescue Exception => e
# Report the generic exception
raise Errors::VagrantfileLoadError,
:path => path,
:message => e.message
end
end end
end end
end end

View File

@ -428,6 +428,10 @@ module Vagrant
error_key(:vagrantfile_exists) error_key(:vagrantfile_exists)
end end
class VagrantfileLoadError < VagrantError
error_key(:vagrantfile_load_error)
end
class VagrantfileSyntaxError < VagrantError class VagrantfileSyntaxError < VagrantError
status_code(41) status_code(41)
error_key(:vagrantfile_syntax_error) error_key(:vagrantfile_syntax_error)

View File

@ -278,6 +278,13 @@ en:
vagrantfile_exists: |- vagrantfile_exists: |-
`Vagrantfile` already exists in this directory. Remove it before `Vagrantfile` already exists in this directory. Remove it before
running `vagrant init`. 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: |- vagrantfile_syntax_error: |-
There is a syntax error in the following Vagrantfile. The syntax error There is a syntax error in the following Vagrantfile. The syntax error
message is reproduced below for convenience: message is reproduced below for convenience:

View File

@ -175,5 +175,10 @@ describe Vagrant::Config::Loader do
expect { instance.set(:file, temporary_file("Vagrant:^Config")) }. expect { instance.set(:file, temporary_file("Vagrant:^Config")) }.
to raise_exception(Vagrant::Errors::VagrantfileSyntaxError) to raise_exception(Vagrant::Errors::VagrantfileSyntaxError)
end 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
end end