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

Class Method Details

.dashboard_url(error_log) ⇒ String

Generate dashboard URL for an error

Parameters:

Returns:

  • (String)

    Full URL to the error detail page



15
16
17
18
# 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"
  "#{base_url}/error_dashboard/errors/#{error_log.id}"
end

.error_source(error_log) ⇒ String

Error source description for PagerDuty

Parameters:

Returns:

  • (String)

    Source description



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

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

Parameters:

  • backtrace (String, Array, nil)

    Raw backtrace

  • limit (Integer) (defaults to: 20)

    Maximum lines to extract (default 20)

Returns:

  • (Array<String>)

    Backtrace lines



33
34
35
36
37
38
# File 'lib/rails_error_dashboard/services/notification_helpers.rb', line 33

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)

Parameters:

  • backtrace (String, Array, nil)

    Raw backtrace

  • length (Integer) (defaults to: 100)

    Maximum length (default 100)

Returns:

  • (String)

    First line or “N/A”



44
45
46
47
48
49
50
51
52
# File 'lib/rails_error_dashboard/services/notification_helpers.rb', line 44

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

Parameters:

  • time (Time, nil)

    Time to format

Returns:

  • (String)

    Formatted time or “N/A”



69
70
71
72
# File 'lib/rails_error_dashboard/services/notification_helpers.rb', line 69

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

Parameters:

  • params_json (String, nil)

    JSON string

Returns:

  • (Hash)

    Parsed params or empty hash



77
78
79
80
81
82
# File 'lib/rails_error_dashboard/services/notification_helpers.rb', line 77

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

Parameters:

  • platform (String, nil)

    Platform name

Returns:

  • (String)

    Emoji



57
58
59
60
61
62
63
64
# File 'lib/rails_error_dashboard/services/notification_helpers.rb', line 57

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

Parameters:

  • message (String, nil)

    The message to truncate

  • length (Integer) (defaults to: 500)

    Maximum length (default 500)

Returns:

  • (String)

    Truncated message



24
25
26
27
# File 'lib/rails_error_dashboard/services/notification_helpers.rb', line 24

def truncate_message(message, length = 500)
  return "" unless message
  message.length > length ? "#{message[0...length]}..." : message
end