Class: Rox::Core::ErrorReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/rox/core/reporting/error_reporter.rb

Constant Summary collapse

BUGSNAG_NOTIFY_URL =
'https://notify.bugsnag.com'.freeze
STACK_TRACE_LINE_REGEX =
/^((?:[a-zA-Z]:)?[^:]+):(\d+)(?::in `([^']+)')?$/.freeze

Instance Method Summary collapse

Constructor Details

#initialize(request, device_properties, buid) ⇒ ErrorReporter

Returns a new instance of ErrorReporter.



8
9
10
11
12
# File 'lib/rox/core/reporting/error_reporter.rb', line 8

def initialize(request, device_properties, buid)
  @request = request
  @device_properties = device_properties
  @buid = buid
end

Instance Method Details

#add_api_key(payload) ⇒ Object



65
66
67
# File 'lib/rox/core/reporting/error_reporter.rb', line 65

def add_api_key(payload)
  payload['apiKey'] = '9569ec14f61546c6aa2a97856492bf4d'
end

#add_app(ev) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/rox/core/reporting/error_reporter.rb', line 123

def add_app(ev)
  app = {
    'releaseStage' => @device_properties.rollout_environment,
    'version' => @device_properties.lib_version
  }
  ev['app'] = app
end

#add_event(message, ex, stack, events) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/rox/core/reporting/error_reporter.rb', line 75

def add_event(message, ex, stack, events)
  ev = {}
  add_payload_version(ev)
  add_exceptions(message, ex, stack, ev)
  add_user('id', @device_properties.rollout_key, ev)
  (message, ev)
  add_app(ev)

  events << ev
end

#add_events(message, ex, stack, payload) ⇒ Object



69
70
71
72
73
# File 'lib/rox/core/reporting/error_reporter.rb', line 69

def add_events(message, ex, stack, payload)
  evs = []
  add_event(message, ex, stack, evs)
  payload['events'] = evs
end

#add_exceptions(message, ex, stack, ev) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rox/core/reporting/error_reporter.rb', line 105

def add_exceptions(message, ex, stack, ev)
  exceptions = []
  exception = {}

  if ex.nil?
    exception['errorClass'] = message
    exception['message'] = message
    exception['stacktrace'] = []
  else
    exception['errorClass'] = ex.message
    exception['message'] = ex.message
    exception['stacktrace'] = stack
  end

  exceptions.append(exception)
  ev['exceptions'] = exceptions
end

#add_metadata(message, ev) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rox/core/reporting/error_reporter.rb', line 51

def (message, ev)
  inner_data = {
    'message' => message,
    'deviceId' => @device_properties.distinct_id,
    'buid' => @buid.to_s
  }

   = {
    'data' => inner_data
  }

  ev['metaData'] = 
end

#add_notifier(payload) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/rox/core/reporting/error_reporter.rb', line 90

def add_notifier(payload)
  notifier = {
    'name' => 'Rollout Ruby SDK',
    'version' => @device_properties.lib_version
  }
  payload['notifier'] = notifier
end

#add_payload_version(ev) ⇒ Object



86
87
88
# File 'lib/rox/core/reporting/error_reporter.rb', line 86

def add_payload_version(ev)
  ev['payloadVersion'] = 2
end

#add_user(id, rollout_key, ev) ⇒ Object



98
99
100
101
102
103
# File 'lib/rox/core/reporting/error_reporter.rb', line 98

def add_user(id, rollout_key, ev)
  user = {
    id => rollout_key
  }
  ev['user'] = user
end

#create_payload(message, ex, stack) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/rox/core/reporting/error_reporter.rb', line 42

def create_payload(message, ex, stack)
  payload = {}
  add_api_key(payload)
  add_notifier(payload)
  add_events(message, ex, stack, payload)

  payload
end

#parse_stack_trace(stack_trace) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/rox/core/reporting/error_reporter.rb', line 133

def parse_stack_trace(stack_trace)
  stack = []
  stack_trace.each do |line|
    match = line.match(STACK_TRACE_LINE_REGEX)
    next if match.nil?

    file = match[1]
    line_str = match[2]
    func = match[3]
    stack << {
      'file' => file,
      'method' => func,
      'lineNumber' => line_str.to_i,
      'columnNumber' => 0
    }
  end
  stack
end

#report(message, ex) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rox/core/reporting/error_reporter.rb', line 14

def report(message, ex)
  return if @device_properties.rollout_environment == 'LOCAL'
  return if @device_properties.rox_options.self_managed?

  Logging.logger.error("Error report: #{message}", ex)

  begin
    stack_trace = ex.nil? ? caller : ex.backtrace
    stack = parse_stack_trace(stack_trace)
    payload = create_payload(message, ex, stack)
  rescue StandardError => e
    Logging.logger.error('failed to create bugsnag json payload of the error', e)
  else
    Thread.new { send_error(payload) }
  end
end

#send_error(payload) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/rox/core/reporting/error_reporter.rb', line 31

def send_error(payload)
  Logging.logger.debug('Sending bugsnag error report...')

  begin
    @request.send_post(ErrorReporter::BUGSNAG_NOTIFY_URL, payload)
    Logging.logger.debug('Bugsnag error report was sent')
  rescue StandardError => e
    Logging.logger.error('Failed to send bugsnag error ', e)
  end
end