class Vagrant::Errors::VagrantError

Main superclass of any errors in Vagrant. This provides some convenience methods for setting the status code and error key. The status code is used by the ‘vagrant` executable as the error code, and the error key is used as a default message from I18n.

Attributes

extra_data[RW]

This is extra data passed into the message for translation.

Public Class Methods

error_key(key=nil, namespace=nil) click to toggle source
# File lib/vagrant/errors.rb, line 49
def self.error_key(key=nil, namespace=nil)
  define_method(:error_key) { key }
  error_namespace(namespace) if namespace
end
error_message(message) click to toggle source
# File lib/vagrant/errors.rb, line 54
def self.error_message(message)
  define_method(:error_message) { message }
end
error_namespace(namespace) click to toggle source
# File lib/vagrant/errors.rb, line 58
def self.error_namespace(namespace)
  define_method(:error_namespace) { namespace }
end
new(*args) click to toggle source
Calls superclass method
# File lib/vagrant/errors.rb, line 62
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] ||= error_key
  message[:_namespace] ||= error_namespace
  message[:_key] = key if key

  if message[:_key]
    message = translate_error(message)
  else
    message = error_message
  end

  super(message)
end

Public Instance Methods

error_key() click to toggle source

The key for the error message. This should be set using the {error_key} method but can be overridden here if needed.

# File lib/vagrant/errors.rb, line 91
def error_key; nil; end
error_message() click to toggle source

The error message for this error. This is used if no error_key is specified for a translatable error message.

# File lib/vagrant/errors.rb, line 82
def error_message; "No error message"; end
error_namespace() click to toggle source

The default error namespace which is used for the error key. This can be overridden here or by calling the “error_namespace” class method.

# File lib/vagrant/errors.rb, line 87
def error_namespace; "vagrant.errors"; end
status_code() click to toggle source

This is the exit code that should be used when exiting from this exception.

@return [Integer]

# File lib/vagrant/errors.rb, line 97
def status_code; 1; end

Protected Instance Methods

translate_error(opts) click to toggle source
# File lib/vagrant/errors.rb, line 101
def translate_error(opts)
  return nil if !opts[:_key]
  I18n.t("#{opts[:_namespace]}.#{opts[:_key]}", **opts)
end