Class: RailsErrorDashboard::Services::DiscordPayloadBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_error_dashboard/services/discord_payload_builder.rb

Overview

Pure algorithm: Build Discord embed payload for error notifications

No HTTP calls — accepts error data, returns a Hash ready for JSON serialization.

Examples:

DiscordPayloadBuilder.call(error_log)
# => { embeds: [...] }

Constant Summary collapse

SEVERITY_COLORS =
{
  critical: 16711680,  # Red
  high: 16744192,      # Orange
  medium: 16776960    # Yellow
}.freeze
DEFAULT_COLOR =

Yellow

8421504

Class Method Summary collapse

Class Method Details

.call(error_log, locale: I18nStore::DEFAULT_LOCALE) ⇒ Hash

Field names are human-readable and localized; "inline", "color" and "timestamp" are Discord embed schema and stay as they are. The footer is the product name, which is not translated.

Parameters:

  • error_log (ErrorLog)

    The error to build a payload for

  • locale (String) (defaults to: I18nStore::DEFAULT_LOCALE)

    resolved by the enqueueing thread (P4-T1)

Returns:

  • (Hash)

    Discord embed payload



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rails_error_dashboard/services/discord_payload_builder.rb', line 27

def self.call(error_log, locale: I18nStore::DEFAULT_LOCALE)
  unknown = NotificationHelpers.unknown(locale)
  not_available = NotificationHelpers.not_available(locale)

  {
    embeds: [ {
      # error_type is an exception class name — verbatim.
      title: NotificationHelpers.t(
        "red.notifications.error_alert.discord_title", locale, error_type: error_log.error_type
      ),
      description: NotificationHelpers.truncate_message(error_log.message, 200),
      color: severity_color(error_log),
      fields: [
        {
          name: NotificationHelpers.label(:application, locale),
          value: NotificationHelpers.app_name(error_log),
          inline: true
        },
        {
          name: NotificationHelpers.label(:platform, locale),
          value: error_log.platform || unknown,
          inline: true
        },
        *environment_fields(error_log, locale),
        {
          name: NotificationHelpers.label(:occurrences, locale),
          value: error_log.occurrence_count.to_s,
          inline: true
        },
        {
          name: NotificationHelpers.label(:controller, locale),
          value: error_log.controller_name || not_available,
          inline: true
        },
        {
          name: NotificationHelpers.label(:action, locale),
          value: error_log.action_name || not_available,
          inline: true
        },
        {
          name: NotificationHelpers.label(:first_seen, locale),
          value: NotificationHelpers.format_datetime_or_na(error_log.first_seen_at, locale),
          inline: true
        },
        {
          # Backtrace content is diagnostic output — never translated.
          name: NotificationHelpers.label(:location, locale),
          value: NotificationHelpers.extract_first_backtrace_line(error_log.backtrace, locale: locale),
          inline: false
        }
      ],
      footer: {
        text: NotificationHelpers.t("red.notifications.shared.product_name", locale)
      },
      timestamp: error_log.occurred_at.iso8601
    } ]
  }
end

.environment_fields(error_log, locale) ⇒ Integer

One field when the error carries an environment, none for a legacy row.

Parameters:

Returns:

  • (Integer)

    Discord color integer



89
90
91
92
93
# File 'lib/rails_error_dashboard/services/discord_payload_builder.rb', line 89

def self.environment_fields(error_log, locale)
  return [] unless error_log.respond_to?(:environment) && error_log.environment.present?

  [ { name: NotificationHelpers.label(:environment, locale), value: error_log.environment, inline: true } ]
end

.severity_color(error_log) ⇒ Object



95
96
97
# File 'lib/rails_error_dashboard/services/discord_payload_builder.rb', line 95

def self.severity_color(error_log)
  SEVERITY_COLORS[error_log.severity] || DEFAULT_COLOR
end