(#9055) Print more helpful error message for NameEror exceptions
This commit adds some additional handling for when Vagrant loads config files. Instead of showing the basic ruby exception, it prints a more helpful error message and tries to direct the user to the line number and file where the exception is occuring.
This commit is contained in:
parent
180a82c6eb
commit
627babe15e
|
@ -122,7 +122,27 @@ module Vagrant
|
|||
|
||||
# Get the proper version loader for this version and load
|
||||
version_loader = @versions.get(version)
|
||||
version_config = version_loader.load(proc)
|
||||
begin
|
||||
version_config = version_loader.load(proc)
|
||||
rescue NameError => e
|
||||
line = "(unknown)"
|
||||
path = "(unknown)"
|
||||
if e.backtrace && e.backtrace[0]
|
||||
backtrace_tokens = e.backtrace[0].split(":")
|
||||
path = backtrace_tokens[0]
|
||||
backtrace_tokens.each do |part|
|
||||
if part =~ /\d+/
|
||||
line = part.to_i
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
raise Errors::VagrantfileNameError,
|
||||
path: path,
|
||||
line: line,
|
||||
message: e.message.sub(/' for .*$/, "'")
|
||||
end
|
||||
|
||||
# Store the errors/warnings associated with loading this
|
||||
# configuration. We'll store these for later.
|
||||
|
|
|
@ -780,6 +780,10 @@ module Vagrant
|
|||
error_key(:vagrantfile_load_error)
|
||||
end
|
||||
|
||||
class VagrantfileNameError < VagrantError
|
||||
error_key(:vagrantfile_name_error)
|
||||
end
|
||||
|
||||
class VagrantfileSyntaxError < VagrantError
|
||||
error_key(:vagrantfile_syntax_error)
|
||||
end
|
||||
|
|
|
@ -1374,6 +1374,14 @@ en:
|
|||
Path: %{path}
|
||||
Line number: %{line}
|
||||
Message: %{exception_class}: %{message}
|
||||
vagrantfile_name_error: |-
|
||||
There was an error loading a Vagrantfile. The file being loaded
|
||||
and the error message are shown below. This is usually caused by
|
||||
an invalid or undefined variable.
|
||||
|
||||
Path: %{path}
|
||||
Line number: %{line}
|
||||
Message: %{message}
|
||||
vagrantfile_syntax_error: |-
|
||||
There is a syntax error in the following Vagrantfile. The syntax error
|
||||
message is reproduced below for convenience:
|
||||
|
|
|
@ -87,6 +87,20 @@ describe Vagrant::Config::Loader do
|
|||
expect(warnings).to eq([])
|
||||
expect(errors).to eq([])
|
||||
end
|
||||
|
||||
it "should throw a NameError exception if invalid or undefined variable is used" do
|
||||
vagrantfile = <<-VF
|
||||
Vagrant.configure("2") do |config|
|
||||
config.ssh.port = variable
|
||||
end
|
||||
VF
|
||||
|
||||
instance.set(:foo, temporary_file(vagrantfile))
|
||||
|
||||
expect {
|
||||
instance.load([:foo])
|
||||
}.to raise_error(Vagrant::Errors::VagrantfileNameError, /invalid or undefined variable/)
|
||||
end
|
||||
end
|
||||
|
||||
describe "finalization" do
|
||||
|
|
Loading…
Reference in New Issue