class Rox::Core::ErrorReporter

Constants

BUGSNAG_NOTIFY_URL
STACK_TRACE_LINE_REGEX

Public Class Methods

new(request, device_properties, buid) click to toggle source
# 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

Public Instance Methods

add_api_key(payload) click to toggle source
# File lib/rox/core/reporting/error_reporter.rb, line 65
def add_api_key(payload)
  payload['apiKey'] = '9569ec14f61546c6aa2a97856492bf4d'
end
add_app(ev) click to toggle source
# 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) click to toggle source
# 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)
  add_metadata(message, ev)
  add_app(ev)

  events << ev
end
add_events(message, ex, stack, payload) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/rox/core/reporting/error_reporter.rb, line 51
def add_metadata(message, ev)
  inner_data = {
    'message' => message,
    'deviceId' => @device_properties.distinct_id,
    'buid' => @buid.to_s
  }

  metadata = {
    'data' => inner_data
  }

  ev['metaData'] = metadata
end
add_notifier(payload) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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