Class: RailsErrorDashboard::Services::SlackPayloadBuilder

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

Overview

Pure algorithm: Build Slack Block Kit payload for error notifications

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

Examples:

SlackPayloadBuilder.call(error_log)
# => { text: "...", blocks: [...] }

Class Method Summary collapse

Class Method Details

.actions_block(error_log, locale) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rails_error_dashboard/services/slack_payload_builder.rb', line 136

def self.actions_block(error_log, locale)
  {
    type: "actions",
    elements: [
      {
        type: "button",
        text: {
          type: "plain_text",
          text: NotificationHelpers.t("red.notifications.error_alert.view_details", locale),
          emoji: true
        },
        url: NotificationHelpers.dashboard_url(error_log),
        style: "primary"
      }
    ]
  }
end

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

Only the human-readable values are localized. Every "type" and "style" below is Slack Block Kit vocabulary that Slack parses — translating one would produce an invalid payload, not a localized one.

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)

    Slack Block Kit payload



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rails_error_dashboard/services/slack_payload_builder.rb', line 20

def self.call(error_log, locale: I18nStore::DEFAULT_LOCALE)
  {
    text: NotificationHelpers.t("red.notifications.error_alert.fallback", locale),
    blocks: [
      header_block(locale),
      fields_block(error_log, locale),
      message_block(error_log, locale),
      user_block(error_log, locale),
      request_block(error_log, locale),
      actions_block(error_log, locale),
      context_block(error_log, locale)
    ].compact
  }
end

.context_block(error_log, locale) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rails_error_dashboard/services/slack_payload_builder.rb', line 154

def self.context_block(error_log, locale)
  {
    type: "context",
    elements: [
      {
        type: "mrkdwn",
        text: NotificationHelpers.t("red.notifications.error_alert.error_id", locale, id: error_log.id)
      }
    ]
  }
end

.environment_fields(error_log, locale) ⇒ Object

One field when the error carries an environment, none for a legacy row — so pre-0.11 notifications render exactly as they did.



80
81
82
83
84
# File 'lib/rails_error_dashboard/services/slack_payload_builder.rb', line 80

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

  [ { type: "mrkdwn", text: NotificationHelpers.field(:environment, error_log.environment, locale) } ]
end

.fields_block(error_log, locale) ⇒ Object



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
# File 'lib/rails_error_dashboard/services/slack_payload_builder.rb', line 46

def self.fields_block(error_log, locale)
  {
    type: "section",
    fields: [
      {
        type: "mrkdwn",
        text: NotificationHelpers.field(:application, NotificationHelpers.app_name(error_log), locale)
      },
      {
        type: "mrkdwn",
        text: NotificationHelpers.field(:error_type, "`#{error_log.error_type}`", locale)
      },
      {
        type: "mrkdwn",
        text: NotificationHelpers.field(
          :platform,
          "#{NotificationHelpers.platform_emoji(error_log.platform)} " \
            "#{error_log.platform || NotificationHelpers.unknown(locale)}",
          locale
        )
      },
      *environment_fields(error_log, locale),
      {
        type: "mrkdwn",
        text: NotificationHelpers.field(
          :occurred, NotificationHelpers.format_datetime(error_log.occurred_at, locale), locale
        )
      }
    ]
  }
end

.header_block(locale) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/rails_error_dashboard/services/slack_payload_builder.rb', line 35

def self.header_block(locale)
  {
    type: "header",
    text: {
      type: "plain_text",
      text: NotificationHelpers.t("red.notifications.error_alert.heading", locale),
      emoji: true
    }
  }
end

.message_block(error_log, locale) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rails_error_dashboard/services/slack_payload_builder.rb', line 86

def self.message_block(error_log, locale)
  {
    type: "section",
    text: {
      type: "mrkdwn",
      # Message content is diagnostic output and is never translated.
      text: NotificationHelpers.field(
        :message, "```#{NotificationHelpers.truncate_message(error_log.message)}```", locale
      )
    }
  }
end

.request_block(error_log, locale) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rails_error_dashboard/services/slack_payload_builder.rb', line 122

def self.request_block(error_log, locale)
  return nil unless error_log.request_url.present?

  {
    type: "section",
    text: {
      type: "mrkdwn",
      text: NotificationHelpers.field(
        :request_url, "`#{NotificationHelpers.truncate_message(error_log.request_url, 200)}`", locale
      )
    }
  }
end

.user_block(error_log, locale) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rails_error_dashboard/services/slack_payload_builder.rb', line 99

def self.user_block(error_log, locale)
  return nil unless error_log.user_id.present?

  user_email = error_log.user&.email ||
    NotificationHelpers.t("red.notifications.error_alert.user_fallback", locale, id: error_log.user_id)

  {
    type: "section",
    fields: [
      {
        type: "mrkdwn",
        text: NotificationHelpers.field(:user, user_email, locale)
      },
      {
        type: "mrkdwn",
        text: NotificationHelpers.field(
          :ip_address, error_log.ip_address || NotificationHelpers.not_available(locale), locale
        )
      }
    ]
  }
end