Class: ExceptionNotifier::ExceptionTrackNotifier

Inherits:
BaseNotifier
  • Object
show all
Defined in:
lib/exception_notifier/exception_track_notifier.rb

Instance Method Summary collapse

Constructor Details

#initialize(_opts) ⇒ ExceptionTrackNotifier

Returns a new instance of ExceptionTrackNotifier.



5
# File 'lib/exception_notifier/exception_track_notifier.rb', line 5

def initialize(_opts); end

Instance Method Details

#call(exception, opts = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/exception_notifier/exception_track_notifier.rb', line 7

def call(exception, opts = {})
  return unless ExceptionTrack.config.enabled_env?(Rails.env)

  # send the notification
  title = exception.message || "None"
  messages = []

  ActiveSupport::Notifications.instrument("track.exception_track", title: title) do
    messages << headers_for_env(opts[:env])
    messages << ""
    messages << "--------------------------------------------------"
    messages << ""
    messages << exception.inspect
    unless exception.backtrace.blank?
      messages << "\n"
      messages << exception.backtrace
    end

    Rails.logger.silence do
      ExceptionTrack::Log.create(title: title[0, 200], body: messages.join("\n"))
    end
  end
rescue StandardError => e
  errs = []
  errs << "-- [ExceptionTrack] create error ---------------------------"
  errs << e.message.indent(2)
  errs << ""
  errs << "-- Exception detail ----------------------------------------"
  errs << title.indent(2)
  errs << ""
  errs << messages.join("\n").indent(2)
  Rails.logger.error errs.join("\n")
end

#filter_parameters(env) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/exception_notifier/exception_track_notifier.rb', line 62

def filter_parameters(env)
  parameters = env["action_dispatch.request.parameters"] || {}
  parameter_filter = ActiveSupport::ParameterFilter.new(env["action_dispatch.parameter_filter"] || [])
  parameter_filter.filter(parameters)
rescue StandardError => e
  Rails.logger.error "filter_parameters error: #{e.inspect}"
  parameters
end

#headers_for_env(env) ⇒ Object

Log Request headers from Rack env



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/exception_notifier/exception_track_notifier.rb', line 42

def headers_for_env(env)
  return "" if env.blank?

  parameters = filter_parameters(env)

  headers = []
  headers << "Method:      #{env['REQUEST_METHOD']}"
  headers << "URL:         #{env['REQUEST_URI']}"
  headers << "Parameters:\n#{pretty_hash(parameters.except(:controller, :action), 13)}" if env["REQUEST_METHOD"].downcase != "get"
  headers << "Controller:  #{parameters['controller']}##{parameters['action']}"
  headers << "RequestId:   #{env['action_dispatch.request_id']}"
  headers << "User-Agent:  #{env['HTTP_USER_AGENT']}"
  headers << "Remote IP:   #{env['REMOTE_ADDR']}"
  headers << "Language:    #{env['HTTP_ACCEPT_LANGUAGE']}"
  headers << "Server:      #{Socket.gethostname}"
  headers << "Process:     #{$PROCESS_ID}"

  headers.join("\n")
end

#pretty_hash(params, indent = 0) ⇒ Object



71
72
73
74
# File 'lib/exception_notifier/exception_track_notifier.rb', line 71

def pretty_hash(params, indent = 0)
  json = JSON.pretty_generate(params)
  json.indent(indent)
end