class ExceptionNotifier::Formatter

Attributes

app_name[R]
env[R]
errors_count[R]
exception[R]

Public Class Methods

new(exception, opts = {}) click to toggle source
# File lib/exception_notifier/modules/formatter.rb, line 12
def initialize(exception, opts = {})
  @exception = exception

  @env = opts[:env]
  @errors_count = opts[:accumulated_errors_count].to_i
  @app_name = opts[:app_name] || rails_app_name
end

Public Instance Methods

backtrace_message() click to toggle source

Backtrace: “‘

  • app/controllers/my_controller.rb:99:in ‘specific_function’

  • app/controllers/my_controller.rb:70:in ‘specific_param’

  • app/controllers/my_controller.rb:53:in ‘my_controller_params’

“‘

# File lib/exception_notifier/modules/formatter.rb, line 86
def backtrace_message
  backtrace = exception.backtrace ? clean_backtrace(exception) : nil

  return unless backtrace

  text = []

  text << '```'
  backtrace.first(3).each { |line| text << "* #{line}" }
  text << '```'

  text.join("\n")
end
controller_and_action() click to toggle source

home#index

# File lib/exception_notifier/modules/formatter.rb, line 103
def controller_and_action
  "#{controller.controller_name}##{controller.action_name}" if controller
end
request_message() click to toggle source

Request: “‘

  • url : www.example.com/

  • http_method : GET

  • ip_address : 127.0.0.1

  • parameters : {“controller”=>“home”, “action”=>“index”}

  • timestamp : 2019-01-01 00:00:00 UTC

“‘

# File lib/exception_notifier/modules/formatter.rb, line 62
def request_message
  request = ActionDispatch::Request.new(env) if env
  return unless request

  [
    '```',
    "* url : #{request.original_url}",
    "* http_method : #{request.method}",
    "* ip_address : #{request.remote_ip}",
    "* parameters : #{request.filtered_parameters}",
    "* timestamp : #{Time.current}",
    '```'
  ].join("\n")
end
subtitle() click to toggle source

A NoMethodError occurred. 3 NoMethodError occurred. A NoMethodError occurred in *home#index*.

# File lib/exception_notifier/modules/formatter.rb, line 39
def subtitle
  errors_text = if errors_count > 1
                  errors_count
                else
                  exception.class.to_s =~ /^[aeiou]/i ? 'An' : 'A'
                end

  in_action = " in *#{controller_and_action}*" if controller

  "#{errors_text} *#{exception.class}* occurred#{in_action}."
end
title() click to toggle source

:warning: Error occurred in production :warning: :warning: Error occurred :warning:

# File lib/exception_notifier/modules/formatter.rb, line 24
def title
  env = Rails.env if defined?(::Rails) && ::Rails.respond_to?(:env)

  if env
    "⚠️ Error occurred in #{env} ⚠️"
  else
    '⚠️ Error occurred ⚠️'
  end
end

Private Instance Methods

controller() click to toggle source
# File lib/exception_notifier/modules/formatter.rb, line 121
def controller
  env['action_controller.instance'] if env
end
rails_app_name() click to toggle source
# File lib/exception_notifier/modules/formatter.rb, line 111
def rails_app_name
  return unless defined?(::Rails) && ::Rails.respond_to?(:application)

  if Rails::VERSION::MAJOR >= 6
    Rails.application.class.module_parent_name.underscore
  else
    Rails.application.class.parent_name.underscore
  end
end