Class: ExceptionNotifier::Formatter

Inherits:
Object
  • Object
show all
Includes:
BacktraceCleaner
Defined in:
lib/exception_notifier/modules/formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BacktraceCleaner

#clean_backtrace

Constructor Details

#initialize(exception, opts = {}) ⇒ Formatter

Returns a new instance of Formatter.



12
13
14
15
16
17
18
# 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

Instance Attribute Details

#app_nameObject (readonly)

Returns the value of attribute app_name.



10
11
12
# File 'lib/exception_notifier/modules/formatter.rb', line 10

def app_name
  @app_name
end

Instance Method Details

#backtrace_messageObject

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’

“‘



86
87
88
89
90
91
92
93
94
95
96
97
98
# 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_actionObject

home#index



103
104
105
# File 'lib/exception_notifier/modules/formatter.rb', line 103

def controller_and_action
  "#{controller.controller_name}##{controller.action_name}" if controller
end

#request_messageObject

Request: “‘

  • url : www.example.com/

  • http_method : GET

  • ip_address : 127.0.0.1

  • parameters : “action”=>“index”

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

“‘



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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

#subtitleObject

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



39
40
41
42
43
44
45
46
47
48
49
# 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

#titleObject

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



24
25
26
27
28
29
30
31
32
# 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