Show proper syntax error for Vagrantfiles [closes GH-155]

This commit is contained in:
Mitchell Hashimoto 2010-09-04 14:33:53 -07:00
parent 53aaa4f264
commit ce9ff73ea4
5 changed files with 5 additions and 23 deletions

View File

@ -9,7 +9,7 @@ begin
rescue Vagrant::Errors::VagrantError => e
opts = { :_translate => false, :_prefix => false }
env.ui.error e.message, opts if e.message
env.ui.error e.backtrace.join("\n"), opts if ENV["VAGRANT_DEBUG"] || e.show_stacktrace
env.ui.error e.backtrace.join("\n"), opts if ENV["VAGRANT_DEBUG"]
exit e.status_code if e.respond_to?(:status_code)
exit 999 # An error occurred with no status code defined
end

View File

@ -54,9 +54,9 @@ module Vagrant
if item.is_a?(String) && File.exist?(item)
begin
load item
rescue SyntaxError
rescue SyntaxError => e
# Report syntax errors in a nice way for Vagrantfiles
raise Errors::VagrantfileSyntaxError.new(:file => item)
raise Errors::VagrantfileSyntaxError.new(:file => e.message)
end
elsif item.is_a?(Proc)
self.class.run(&item)

View File

@ -29,10 +29,6 @@ module Vagrant
define_method(:error_namespace) { namespace }
end
def self.force_stacktrace
define_method(:show_stacktrace) { true }
end
def initialize(message=nil, *args)
message = { :_key => message } if message && !message.is_a?(Hash)
message = { :_key => error_key, :_namespace => error_namespace }.merge(message || {})
@ -50,9 +46,6 @@ module Vagrant
# {error_key} method but can be overridden here if needed.
def error_key; nil; end
# Force the stacktrace to show (false by default)
def show_stacktrace; false; end
protected
def translate_error(opts)
@ -219,7 +212,6 @@ module Vagrant
class VagrantfileSyntaxError < VagrantError
status_code(41)
error_key(:vagrantfile_syntax_error)
force_stacktrace
end
class VirtualBoxInvalidOSE < VagrantError

View File

@ -57,11 +57,10 @@ en:
A VM system type must be specified! This is done via the `config.vm.system`
configuration value. Please read the documentation online for more information.
vagrantfile_syntax_error: |-
There is a syntax error in the following Vagrantfile. The stack trace
is also printed below for convenience.
There is a syntax error in the following Vagrantfile. The syntax error
message is reproduced below for convenience:
%{file}
virtualbox_invalid_ose: |-
Vagrant has detected you're using an OSE ("Open Source Edition") of VirtualBox.
Vagrant currently doesn't support any of the OSE editions due to slight API

View File

@ -39,13 +39,4 @@ class ErrorsTest < Test::Unit::TestCase
klass = Class.new(@super) { error_key(:test_key_with_interpolation) }
assert_equal I18n.t("vagrant.test.errors.test_key_with_interpolation", :key => "yo"), klass.new(:key => "yo").message
end
should "not force stacktrace show by default" do
assert !@super.new.show_stacktrace
end
should "force stacktrace to show if enabled" do
klass = Class.new(@super) { force_stacktrace }
assert klass.new.show_stacktrace
end
end