core: errors expose their extra data
This commit is contained in:
parent
1f6095f912
commit
b15cb22e3e
|
@ -43,6 +43,9 @@ module Vagrant
|
|||
# error code, and the error key is used as a default message from
|
||||
# I18n.
|
||||
class VagrantError < StandardError
|
||||
# This is extra data passed into the message for translation.
|
||||
attr_accessor :extra_data
|
||||
|
||||
def self.error_key(key=nil, namespace=nil)
|
||||
define_method(:error_key) { key }
|
||||
error_namespace(namespace) if namespace
|
||||
|
@ -57,6 +60,8 @@ module Vagrant
|
|||
end
|
||||
|
||||
def initialize(message=nil, *args)
|
||||
message ||= {}
|
||||
@extra_data = message.dup
|
||||
message = { :_key => message } if message && !message.is_a?(Hash)
|
||||
message = { :_key => error_key, :_namespace => error_namespace }.merge(message || {})
|
||||
|
||||
|
|
|
@ -32,10 +32,16 @@ describe Vagrant::Errors::VagrantError do
|
|||
end
|
||||
end
|
||||
|
||||
subject { klass.new }
|
||||
subject { klass.new(data: "yep") }
|
||||
|
||||
it "should use the translation for the message" do
|
||||
subject.to_s.should == "foo"
|
||||
end
|
||||
|
||||
it "should expose translation keys to the user" do
|
||||
expect(subject.extra_data.length).to eql(1)
|
||||
expect(subject.extra_data).to have_key(:data)
|
||||
expect(subject.extra_data[:data]).to eql("yep")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue