Class: ESM::Message::Error

Inherits:
Object
  • Object
show all
Defined in:
lib/esm/message/error.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, type, content) ⇒ Error

Returns a new instance of Error.



8
9
10
11
12
# File 'lib/esm/message/error.rb', line 8

def initialize(message, type, content)
  @message = message
  @type = type.to_sym
  @content = content.to_s
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



6
7
8
# File 'lib/esm/message/error.rb', line 6

def content
  @content
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/esm/message/error.rb', line 6

def type
  @type
end

Instance Method Details

#to_hObject



14
15
16
17
18
19
# File 'lib/esm/message/error.rb', line 14

def to_h
  {
    type: @type,
    content: @content
  }
end

#to_sObject



21
22
23
24
25
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
# File 'lib/esm/message/error.rb', line 21

def to_s
  case type
  when :code
     = @message.

    replacements = {
      message_id: @message.id,
      type: @message.type,
      server_id: .server_id,
      user: .player&.discord_mention,
      target: .target&.discord_mention
    }

    # Add the data and metadata to the replacements
    # For example, if data has two attributes: "steam_uid" and "discord_id", this will define two replacements:
    #     "data_steam_uid", and "data_discord_id"
    @message.data.to_h.each do |key, value|
      replacements[:"data_#{key}"] = value
    end

    # Call the exception with the replacements
    I18n.t(
      "exceptions.extension.#{content}",
      default: I18n.t("exceptions.extension.default", type: content),
      **replacements
    )
  when :message
    content
  else
    I18n.t("exceptions.extension.default", type: type)
  end
end