core: errors can use error_message to specify string error message
This commit is contained in:
parent
a346976abb
commit
00f61e67e1
|
@ -48,6 +48,10 @@ module Vagrant
|
||||||
error_namespace(namespace) if namespace
|
error_namespace(namespace) if namespace
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.error_message(message)
|
||||||
|
define_method(:error_message) { message }
|
||||||
|
end
|
||||||
|
|
||||||
def self.error_namespace(namespace)
|
def self.error_namespace(namespace)
|
||||||
define_method(:error_namespace) { namespace }
|
define_method(:error_namespace) { namespace }
|
||||||
end
|
end
|
||||||
|
@ -55,11 +59,20 @@ module Vagrant
|
||||||
def initialize(message=nil, *args)
|
def initialize(message=nil, *args)
|
||||||
message = { :_key => message } if message && !message.is_a?(Hash)
|
message = { :_key => message } if message && !message.is_a?(Hash)
|
||||||
message = { :_key => error_key, :_namespace => error_namespace }.merge(message || {})
|
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
|
super
|
||||||
end
|
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.
|
# The default error namespace which is used for the error key.
|
||||||
# This can be overridden here or by calling the "error_namespace"
|
# This can be overridden here or by calling the "error_namespace"
|
||||||
# class method.
|
# class method.
|
||||||
|
|
|
@ -572,6 +572,7 @@ en:
|
||||||
Port: %{port}
|
Port: %{port}
|
||||||
Username: %{username}
|
Username: %{username}
|
||||||
Private key: %{key_path}
|
Private key: %{key_path}
|
||||||
|
test_key: "test value"
|
||||||
ui_expects_tty: |-
|
ui_expects_tty: |-
|
||||||
Vagrant is attempting to interface with the UI in a way that requires
|
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
|
a TTY. Most actions in Vagrant that require a TTY have configuration
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue