Module: RailsErrorDashboard::Services::NotificationHelpers
- Defined in:
- lib/rails_error_dashboard/services/notification_helpers.rb
Overview
Shared helper methods for notification payload builders
Pure functions: no side effects, no HTTP calls, no database access. Used by all notification payload builders to avoid duplication.
Class Method Summary collapse
-
.app_name(error_log) ⇒ String
Application name for notifications.
-
.dashboard_url(error_log) ⇒ String
Generate dashboard URL for an error.
-
.error_source(error_log) ⇒ String
Error source description for PagerDuty.
-
.extract_backtrace(backtrace, limit = 20) ⇒ Array<String>
Extract backtrace lines as an array.
-
.extract_first_backtrace_line(backtrace, length = 100) ⇒ String
Extract first backtrace line (truncated).
-
.format_time(time) ⇒ String
Format time for display.
-
.parse_request_params(params_json) ⇒ Hash
Parse request params JSON safely.
-
.platform_emoji(platform) ⇒ String
Platform emoji for Slack/text notifications.
-
.truncate_message(message, length = 500) ⇒ String
Truncate a message to a maximum length.
Class Method Details
.app_name(error_log) ⇒ String
Application name for notifications
95 96 97 |
# File 'lib/rails_error_dashboard/services/notification_helpers.rb', line 95 def app_name(error_log) error_log.application&.name || "Unknown" end |
.dashboard_url(error_log) ⇒ String
Generate dashboard URL for an error
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rails_error_dashboard/services/notification_helpers.rb', line 15 def dashboard_url(error_log) base_url = (RailsErrorDashboard.configuration.dashboard_base_url || "http://localhost:3000").chomp("/") mount_path = RailsErrorDashboard.configuration.engine_mount_path # Avoid doubling the mount path when base_url already includes it. # e.g., base_url="https://app.com/red" + mount_path="/red" → don't produce "/red/red" if mount_path.present? && base_url.end_with?(mount_path.chomp("/")) "#{base_url}/errors/#{error_log.id}" else "#{base_url}#{mount_path}/errors/#{error_log.id}" end end |
.error_source(error_log) ⇒ String
Error source description for PagerDuty
102 103 104 105 106 107 108 109 110 |
# File 'lib/rails_error_dashboard/services/notification_helpers.rb', line 102 def error_source(error_log) if error_log.controller_name && error_log.action_name "#{error_log.controller_name}##{error_log.action_name}" elsif error_log.request_url error_log.request_url else error_log.platform || "Rails Application" end end |
.extract_backtrace(backtrace, limit = 20) ⇒ Array<String>
Extract backtrace lines as an array
41 42 43 44 45 46 |
# File 'lib/rails_error_dashboard/services/notification_helpers.rb', line 41 def extract_backtrace(backtrace, limit = 20) return [] if backtrace.nil? lines = backtrace.is_a?(String) ? backtrace.lines : backtrace lines.first(limit).map(&:strip) end |
.extract_first_backtrace_line(backtrace, length = 100) ⇒ String
Extract first backtrace line (truncated)
52 53 54 55 56 57 58 59 60 |
# File 'lib/rails_error_dashboard/services/notification_helpers.rb', line 52 def extract_first_backtrace_line(backtrace, length = 100) return "N/A" if backtrace.nil? lines = backtrace.is_a?(String) ? backtrace.lines : backtrace first_line = lines.first&.strip return "N/A" if first_line.nil? first_line.length > length ? "#{first_line[0...length]}..." : first_line end |
.format_time(time) ⇒ String
Format time for display
77 78 79 80 |
# File 'lib/rails_error_dashboard/services/notification_helpers.rb', line 77 def format_time(time) return "N/A" if time.nil? time.strftime("%Y-%m-%d %H:%M:%S UTC") end |
.parse_request_params(params_json) ⇒ Hash
Parse request params JSON safely
85 86 87 88 89 90 |
# File 'lib/rails_error_dashboard/services/notification_helpers.rb', line 85 def parse_request_params(params_json) return {} if params_json.nil? JSON.parse(params_json) rescue JSON::ParserError {} end |
.platform_emoji(platform) ⇒ String
Platform emoji for Slack/text notifications
65 66 67 68 69 70 71 72 |
# File 'lib/rails_error_dashboard/services/notification_helpers.rb', line 65 def platform_emoji(platform) case platform&.downcase when "ios" then "📱" when "android" then "🤖" when "api" then "🔌" else "💻" end end |
.truncate_message(message, length = 500) ⇒ String
Truncate a message to a maximum length
32 33 34 35 |
# File 'lib/rails_error_dashboard/services/notification_helpers.rb', line 32 def (, length = 500) return "" unless .length > length ? "#{[0...length]}..." : end |