core: errors can use error_message to specify string error message

This commit is contained in:
Mitchell Hashimoto 2013-10-01 21:45:05 -07:00
parent a346976abb
commit 00f61e67e1
3 changed files with 48 additions and 1 deletions

View File

@ -48,6 +48,10 @@ module Vagrant
error_namespace(namespace) if namespace
end
def self.error_message(message)
define_method(:error_message) { message }
end
def self.error_namespace(namespace)
define_method(:error_namespace) { namespace }
end
@ -55,11 +59,20 @@ module Vagrant
def initialize(message=nil, *args)
message = { :_key => message } if message && !message.is_a?(Hash)
message = { :_key => error_key, :_namespace => error_namespace }.merge(message || {})
message = translate_error(message)
if error_key
message = translate_error(message)
else
message = error_message
end
super
end
# The error message for this error. This is used if no error_key
# is specified for a translatable error message.
def error_message; "No error message"; end
# The default error namespace which is used for the error key.
# This can be overridden here or by calling the "error_namespace"
# class method.

View File

@ -572,6 +572,7 @@ en:
Port: %{port}
Username: %{username}
Private key: %{key_path}
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

View File

@ -0,0 +1,33 @@
require File.expand_path("../../base", __FILE__)
describe Vagrant::Errors::VagrantError do
describe "subclass with error key" do
let(:klass) do
Class.new(described_class) do
error_key("test_key")
end
end
subject { klass.new }
it "should use the translation for the message" do
subject.to_s.should == "test value"
end
its("status_code") { should eq(1) }
end
describe "subclass with error message" do
let(:klass) do
Class.new(described_class) do
error_message("foo")
end
end
subject { klass.new }
it "should use the translation for the message" do
subject.to_s.should == "foo"
end
end
end