From 7631a386018ab56e92cafa83ae9fc4cffd9e8b26 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 8 Jan 2014 19:53:04 -0800 Subject: [PATCH] core: refactor error initialization logic /cc @tmatilai - This includes a test case for the symbol stuff, should be okay now. --- lib/vagrant/errors.rb | 13 ++++++++----- templates/locales/en.yml | 2 +- test/unit/vagrant/errors_test.rb | 6 ++++++ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 814522b02..21ac8b98a 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -59,11 +59,14 @@ module Vagrant define_method(:error_namespace) { namespace } end - def initialize(message=nil, *args) + def initialize(*args) + key = args.shift if args.first.is_a?(Symbol) + message = args.shift if args.first.is_a?(Hash) message ||= {} - @extra_data = message.dup - message = { :_key => message } if message && !message.is_a?(Hash) - message = { :_key => error_key, :_namespace => error_namespace }.merge(message || {}) + @extra_data = message.dup + message[:_key] ||= error_key + message[:_namespace] ||= error_namespace + message[:_key] = key if key if message[:_key] message = translate_error(message) @@ -71,7 +74,7 @@ module Vagrant message = error_message end - super + super(message) end # The error message for this error. This is used if no error_key diff --git a/templates/locales/en.yml b/templates/locales/en.yml index ad305fec8..2479c6ac4 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -708,7 +708,6 @@ en: The synced folder type '%{type}' is reporting as unusable for your current setup. Please verify you have all the proper prerequisites for using this shared folder type and try again. - test_key: "test value" ui_expects_tty: |- Vagrant is attempting to interface with the UI in a way that requires a TTY. Most actions in Vagrant that require a TTY have configuration @@ -816,6 +815,7 @@ en: vm_not_running: |- VM must be running to open SSH connection. Run `vagrant up` to start the virtual machine. + test_key: "test value" #------------------------------------------------------------------------------- # Translations for config validation errors diff --git a/test/unit/vagrant/errors_test.rb b/test/unit/vagrant/errors_test.rb index 3c00b39c9..fb5423bd0 100644 --- a/test/unit/vagrant/errors_test.rb +++ b/test/unit/vagrant/errors_test.rb @@ -43,5 +43,11 @@ describe Vagrant::Errors::VagrantError do expect(subject.extra_data).to have_key(:data) expect(subject.extra_data[:data]).to eql("yep") end + + it "should use a symbol initializer as a key" do + subject = klass.new(:test_key) + expect(subject.extra_data).to be_empty + expect(subject.to_s).to eql("test value") + end end end