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) ⇒ Object



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

def self.actions_block(error_log)
  {
    type: "actions",
    elements: [
      {
        type: "button",
        text: {
          type: "plain_text",
          text: "View Details",
          emoji: true
        },
        url: NotificationHelpers.dashboard_url(error_log),
        style: "primary"
      }
    ]
  }
end

.call(error_log) ⇒ Hash

Returns Slack Block Kit payload.

Parameters:

  • error_log (ErrorLog)

    The error to build a payload for

Returns:

  • (Hash)

    Slack Block Kit payload



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rails_error_dashboard/services/slack_payload_builder.rb', line 15

def self.call(error_log)
  {
    text: "🚨 New Error Alert",
    blocks: [
      header_block,
      fields_block(error_log),
      message_block(error_log),
      user_block(error_log),
      request_block(error_log),
      actions_block(error_log),
      context_block(error_log)
    ].compact
  }
end

.context_block(error_log) ⇒ Object



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

def self.context_block(error_log)
  {
    type: "context",
    elements: [
      {
        type: "mrkdwn",
        text: "Error ID: #{error_log.id}"
      }
    ]
  }
end

.fields_block(error_log) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rails_error_dashboard/services/slack_payload_builder.rb', line 41

def self.fields_block(error_log)
  {
    type: "section",
    fields: [
      {
        type: "mrkdwn",
        text: "*Error Type:*\n`#{error_log.error_type}`"
      },
      {
        type: "mrkdwn",
        text: "*Platform:*\n#{NotificationHelpers.platform_emoji(error_log.platform)} #{error_log.platform || 'Unknown'}"
      },
      {
        type: "mrkdwn",
        text: "*Occurred:*\n#{error_log.occurred_at.strftime('%B %d, %Y at %I:%M %p')}"
      }
    ]
  }
end

.header_blockObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/rails_error_dashboard/services/slack_payload_builder.rb', line 30

def self.header_block
  {
    type: "header",
    text: {
      type: "plain_text",
      text: "🚨 Error Alert",
      emoji: true
    }
  }
end

.message_block(error_log) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/rails_error_dashboard/services/slack_payload_builder.rb', line 61

def self.message_block(error_log)
  {
    type: "section",
    text: {
      type: "mrkdwn",
      text: "*Message:*\n```#{NotificationHelpers.truncate_message(error_log.message)}```"
    }
  }
end

.request_block(error_log) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rails_error_dashboard/services/slack_payload_builder.rb', line 91

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

  {
    type: "section",
    text: {
      type: "mrkdwn",
      text: "*Request URL:*\n`#{NotificationHelpers.truncate_message(error_log.request_url, 200)}`"
    }
  }
end

.user_block(error_log) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rails_error_dashboard/services/slack_payload_builder.rb', line 71

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

  user_email = error_log.user&.email || "User ##{error_log.user_id}"

  {
    type: "section",
    fields: [
      {
        type: "mrkdwn",
        text: "*User:*\n#{user_email}"
      },
      {
        type: "mrkdwn",
        text: "*IP Address:*\n#{error_log.ip_address || 'N/A'}"
      }
    ]
  }
end