Merge pull request #9252 from briancain/9055/master/pretty-print-nameerror-exceptions
Print more helpful error message for NameEror exceptions
This commit is contained in:
commit
144c402f3b
|
@ -122,7 +122,27 @@ module Vagrant
|
||||||
|
|
||||||
# Get the proper version loader for this version and load
|
# Get the proper version loader for this version and load
|
||||||
version_loader = @versions.get(version)
|
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
|
# Store the errors/warnings associated with loading this
|
||||||
# configuration. We'll store these for later.
|
# configuration. We'll store these for later.
|
||||||
|
|
|
@ -780,6 +780,10 @@ module Vagrant
|
||||||
error_key(:vagrantfile_load_error)
|
error_key(:vagrantfile_load_error)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class VagrantfileNameError < VagrantError
|
||||||
|
error_key(:vagrantfile_name_error)
|
||||||
|
end
|
||||||
|
|
||||||
class VagrantfileSyntaxError < VagrantError
|
class VagrantfileSyntaxError < VagrantError
|
||||||
error_key(:vagrantfile_syntax_error)
|
error_key(:vagrantfile_syntax_error)
|
||||||
end
|
end
|
||||||
|
|
|
@ -1374,6 +1374,14 @@ en:
|
||||||
Path: %{path}
|
Path: %{path}
|
||||||
Line number: %{line}
|
Line number: %{line}
|
||||||
Message: %{exception_class}: %{message}
|
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: |-
|
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:
|
||||||
|
|
|
@ -87,6 +87,20 @@ describe Vagrant::Config::Loader do
|
||||||
expect(warnings).to eq([])
|
expect(warnings).to eq([])
|
||||||
expect(errors).to eq([])
|
expect(errors).to eq([])
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
describe "finalization" do
|
describe "finalization" do
|
||||||
|
|
Loading…
Reference in New Issue