Class: ExceptionNotifier::SimplepushNotifier::SimplepushExceptionEvent

Inherits:
Object
  • Object
show all
Includes:
BacktraceCleaner
Defined in:
lib/integrations/simplepush_notifier.rb

Overview

This class is responsible to format title and message from given exception and options.

Adapted from github.com/smartinez87/exception_notification/blob/master/lib/exception_notifier/datadog_notifier.rb

Version: committed on 27 Dec 2019

Released under MIT license: github.com/smartinez87/exception_notification/blob/master/MIT-LICENSE

Constant Summary collapse

MAX_TITLE_LENGTH =
120
MAX_VALUE_LENGTH =
300
MAX_BACKTRACE_SIZE =
3
MAX_TOTAL_SIZE_BYTES =
1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exception, options) ⇒ SimplepushExceptionEvent

Returns a new instance of SimplepushExceptionEvent.



49
50
51
52
# File 'lib/integrations/simplepush_notifier.rb', line 49

def initialize(exception, options)
  @exception = exception
  @options = options
end

Instance Attribute Details

#exceptionObject (readonly)

Returns the value of attribute exception.



46
47
48
# File 'lib/integrations/simplepush_notifier.rb', line 46

def exception
  @exception
end

#optionsObject (readonly)

Returns the value of attribute options.



46
47
48
# File 'lib/integrations/simplepush_notifier.rb', line 46

def options
  @options
end

Instance Method Details

#backtraceObject



62
63
64
# File 'lib/integrations/simplepush_notifier.rb', line 62

def backtrace
  @backtrace ||= exception.backtrace ? clean_backtrace(exception) : []
end

#controllerObject



58
59
60
# File 'lib/integrations/simplepush_notifier.rb', line 58

def controller
  @controller ||= options[:env] && options[:env]['action_controller.instance']
end

#controller_subtitleObject



150
151
152
# File 'lib/integrations/simplepush_notifier.rb', line 150

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

#formatted_backtraceObject



126
127
128
129
130
131
132
133
134
135
# File 'lib/integrations/simplepush_notifier.rb', line 126

def formatted_backtrace
  size = [backtrace.size, MAX_BACKTRACE_SIZE].min

  text = []
  text << '# Backtrace'
  text << '````'
  size.times { |i| text << backtrace[i] }
  text << '````'
  text.join("\n")
end

#formatted_bodyObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/integrations/simplepush_notifier.rb', line 77

def formatted_body
  text = []

  text << formatted_backtrace
  text << formatted_request if request
  text << formatted_session if request

  text = text.join("\n------------------\n")

  if text.bytesize >= MAX_TOTAL_SIZE_BYTES
    # puts "Text is too long (#{text.bytesize} bytes >= MAX_TOTAL_SIZE), need to truncate"
    
    # if truncate bytes is available
    if text.respond_to?(:truncate_bytes) && false            
      text = text.truncate_bytes(MAX_TOTAL_SIZE_BYTES)
    else
      text = text[0...(MAX_TOTAL_SIZE_BYTES/2)]
    end
    # puts "Text after truncate is #{text.bytesize} bytes <= MAX_TOTAL_SIZE"
  end

  text
end

#formatted_key_value(key, value) ⇒ Object



101
102
103
# File 'lib/integrations/simplepush_notifier.rb', line 101

def formatted_key_value(key, value)
  "#{key}: #{value}"
end

#formatted_requestObject



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/integrations/simplepush_notifier.rb', line 105

def formatted_request
  text = []
  text << '# Request'
  text << formatted_key_value('URL', request.url)
  text << formatted_key_value('HTTP Method', request.request_method)
  text << formatted_key_value('IP Address', request.remote_ip)
  text << formatted_key_value('Parameters', request.filtered_parameters.inspect)
  text << formatted_key_value('Timestamp', Time.current)
  text << formatted_key_value('Server', Socket.gethostname)
  text << formatted_key_value('Rails root', Rails.root) if defined?(Rails) && Rails.respond_to?(:root)
  text << formatted_key_value('Process', $PROCESS_ID)
  text.join("\n")
end

#formatted_sessionObject



119
120
121
122
123
124
# File 'lib/integrations/simplepush_notifier.rb', line 119

def formatted_session
  text = []
  text << '# Session'
  text << formatted_key_value('Data', request.session.to_hash)
  text.join("\n")
end

#formatted_titleObject



70
71
72
73
74
75
# File 'lib/integrations/simplepush_notifier.rb', line 70

def formatted_title
  title =
    "#{title_prefix}#{controller_subtitle} (#{exception.class}) #{exception.message.inspect}"

  truncate(title, MAX_TITLE_LENGTH)
end

#inspect_object(object) ⇒ Object



141
142
143
144
145
146
147
148
# File 'lib/integrations/simplepush_notifier.rb', line 141

def inspect_object(object)
  case object
  when Hash, Array
    truncate(object.inspect, MAX_VALUE_LENGTH)
  else
    object.to_s
  end
end

#requestObject



54
55
56
# File 'lib/integrations/simplepush_notifier.rb', line 54

def request
  @request ||= ActionDispatch::Request.new(options[:env]) if options[:env]
end

#title_prefixObject



66
67
68
# File 'lib/integrations/simplepush_notifier.rb', line 66

def title_prefix
  options[:title_prefix] || ''
end

#truncate(string, max) ⇒ Object



137
138
139
# File 'lib/integrations/simplepush_notifier.rb', line 137

def truncate(string, max)
  string.length > max ? "#{string[0...max]}..." : string
end