Class: ChefCore::Text::ErrorTranslation

Inherits:
Object
  • Object
show all
Defined in:
lib/chef_core/text/error_translation.rb

Overview

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

Defined Under Namespace

Classes: InvalidDisplayAttributes

Constant Summary collapse

ATTRIBUTES =
i{decorations header footer stack log}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(id, params: []) ⇒ ErrorTranslation



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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

Instance Method Details

#_sym(hash) ⇒ Object



55
56
57
# File 'lib/chef_core/text/error_translation.rb', line 55

def _sym(hash)
  hash.map { |k, v| [k.to_sym, v] }.to_h
end

#inspectObject



59
60
61
62
63
64
65
66
# 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