Class: Exceptify::Formatter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BacktraceCleaner

#clean_backtrace

Constructor Details

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

Returns a new instance of Formatter.



13
14
15
16
17
18
19
20
21
22
# File 'lib/exceptify/modules/formatter.rb', line 13

def initialize(exception_or_notification, opts = {})
  @notification = if exception_or_notification.is_a?(Notification)
    exception_or_notification
  else
    Notification.new(exception_or_notification, opts, backtrace_cleaner: self)
  end
  @exception = notification.exception
  @errors_count = notification.options[:accumulated_errors_count].to_i
  @app_name = notification.app_name
end

Instance Attribute Details

#app_nameObject (readonly)

Returns the value of attribute app_name.



11
12
13
# File 'lib/exceptify/modules/formatter.rb', line 11

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'


90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/exceptify/modules/formatter.rb', line 90

def backtrace_message
  backtrace = notification.backtrace

  return if backtrace.empty?

  text = []

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

  text.join("\n")
end

#controller_and_actionObject

home#index



107
108
109
# File 'lib/exceptify/modules/formatter.rb', line 107

def controller_and_action
  notification.controller_and_action
end

#request_messageObject

Request:

* url : https://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


66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/exceptify/modules/formatter.rb', line 66

def request_message
  request = notification.request_context.request
  return unless request

  [
    "```",
    "* url : #{request.original_url}",
    "* http_method : #{request.method}",
    "* ip_address : #{request.remote_ip}",
    "* parameters : #{request.filtered_parameters}",
    "* timestamp : #{notification.timestamp}",
    "```"
  ].join("\n")
end

#subtitleObject

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



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/exceptify/modules/formatter.rb', line 43

def subtitle
  errors_text = if errors_count > 1
    errors_count
  else
    /^[aeiou]/i.match?(exception.class.to_s) ? "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:



28
29
30
31
32
33
34
35
36
# File 'lib/exceptify/modules/formatter.rb', line 28

def title
  env = notification.env_name

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