class ChefCore::Text::ErrorTranslation

Represents an error loaded from translation, with display attributes set.

Constants

ATTRIBUTES

Attributes

message[R]

Public Class Methods

new(id, params: []) click to toggle source
# File lib/chef_core/text/error_translation.rb, line 26
def initialize(id, params: [])
  error_translation = error_translation_for_id(id)

  options = _sym(YAML.load(Text.errors.display_defaults, "display_defaults"))

  # Display metadata is a string containing a YAML hash that is optionally under
  # the error's 'options' attribute
  # Note that we couldn't use :display, as that conflicts with a method on Object.
  display_opts = if error_translation.methods.include?(:options)
                   _sym(YAML.load(error_translation.options, @id))
                 else
                   {}
                 end

  options = options.merge(display_opts) unless display_opts.nil?

  @message = error_translation.text(*params)

  ATTRIBUTES.each do |attribute|
    instance_variable_set("@#{attribute}", options.delete(attribute))
  end

  if options.length > 0
    # Anything not in ATTRIBUTES is not supported. This will also catch
    # typos in attr names
    raise InvalidDisplayAttributes.new(id, options)
  end
end

Public Instance Methods

_sym(hash) click to toggle source
# File lib/chef_core/text/error_translation.rb, line 55
def _sym(hash)
  hash.map { |k, v| [k.to_sym, v] }.to_h
end
inspect() click to toggle source
# File lib/chef_core/text/error_translation.rb, line 59
def inspect
  inspection = "#{self}: "
  ATTRIBUTES.each do |attribute|
    inspection << "#{attribute}: #{send(attribute.to_s)}; "
  end
  inspection << "message: #{message.gsub("\n", "\\n")}"
  inspection
end

Private Instance Methods

error_translation_for_id(id) click to toggle source

This is split out for simplified unit testing of error formatting.

# File lib/chef_core/text/error_translation.rb, line 71
def error_translation_for_id(id)
  Text.errors.send(id)
end