class FacebookAds::CrashLogger

Public Class Methods

disable() click to toggle source

Disable crash reporting

# File lib/facebook_ads/crash_logger.rb, line 128
def disable
  @@enabled = false
end
enable() click to toggle source

Enable crash reporting

# File lib/facebook_ads/crash_logger.rb, line 115
def enable
  if !@@hasRegisteredCrashBlock then
    at_exit do
      completion()
    end

    @@hasRegisteredCrashBlock = true
  end

  @@enabled = true
end

Private Class Methods

completion() click to toggle source
# File lib/facebook_ads/crash_logger.rb, line 64
def completion
  if @@enabled && $! then
    logCrash()
  end
end
formatted_reason(error) click to toggle source
# File lib/facebook_ads/crash_logger.rb, line 76
def formatted_reason(error)
  if error.kind_of? APIError
    reason = 'Server Error' if error.kind_of? ServerError
    reason = 'Client Error' if error.kind_of? ClientError
    reason += " #{error.message}"
  else
    reason = 'SDK Error'
  end
  reason
end
get_app_id() click to toggle source
# File lib/facebook_ads/crash_logger.rb, line 87
def get_app_id
  params = {
    'input_token': Session.default_session.access_token,
    'fields': 'app_id'
  }
  begin
    APIRequest.new(
      :get,
      '/debug_token',
      session: Session.default_session,
      params: params,
      options: {} # batch options, not necessary for now
    ).execute do |response|
      if (response.result.has_key?('data') &&
        response.result['data'].has_key?('app_id')
      )
        response.result['data']['app_id']
      else
        nil
      end
    end
  rescue
    nil
  end
end
is_facebook_error(error) click to toggle source
# File lib/facebook_ads/crash_logger.rb, line 70
def is_facebook_error(error)
  error.backtrace.any? { |frame|
    frame.match? 'facebook_ads'
  } if error
end
logCrash() click to toggle source
# File lib/facebook_ads/crash_logger.rb, line 29
def logCrash
  error = $!
  if is_facebook_error(error)
    app_id = get_app_id
    if app_id.nil?
      puts "Unable to log the crash: We're missing an app id."
      return
    end
    params = {
      'bizsdk_crash_report': {
        'reason': formatted_reason(error),
        'platform': "ruby #{RUBY_VERSION}",
        'callstack': error.backtrace
      }
    }
    begin
      APIRequest.new(
        :post,
        "#{app_id}/instruments",
        session: Session.anonymous_session,
        params: params,
        options: {} # batch options, not necessary for now
      ).execute do |response|
        if response.result.has_key?('success') && response.result['success'] == true
          puts 'Successfully sent report'
          return
        end
        puts 'Failed to send report'
      end
    rescue
      puts 'Failed to send report'
    end
  end
end