Repository URL to install this package:
| 
      
        
        
        Version: 
        
         
          
          0.9.19  ▾
        
         | 
module ActionSprout::Facebook
  class ErrorHandler
    def self.call(response, access_token, **options)
      self.new(response, access_token, **options).examine
    end
    attr_reader :response, :access_token,
     :app_rate_limited, :page_rate_limited,
     :page_migrated, :not_found, :authentication_error
    def initialize(
        response, access_token,
        app_rate_limited: default_delegate,
        page_rate_limited: default_delegate,
        page_migrated: default_delegate,
        not_found: default_delegate,
        authentication_error: default_delegate
      )
      @response = BadResponse.from_response response
      @access_token = access_token
      @app_rate_limited = app_rate_limited
      @page_rate_limited = page_rate_limited
      @page_migrated = page_migrated
      @not_found = not_found
      @authentication_error = authentication_error
    end
    def examine
      check_app_rate_limit
      check_page_rate_limit
      raise_error_if_needed
    end
    private
    def check_app_rate_limit
      if response.app_rate_limited?
        app_rate_limited.call error: nil, response: response
      end
    end
    def check_page_rate_limit
      if response.page_rate_limited?
        page_rate_limited.call error: nil, response: response
      end
    end
    def raise_error_if_needed
      return unless response.has_error?
      if response.authentication_error?
        error = AuthenticationError.new response.message, response
        authentication_error.call error: error, response: response
        raise error
      elsif response.page_migrated?
        error = PageMigratedError.new response.message, response, **migrated_ids
        page_migrated.call error: error, response: response, **migrated_ids
        raise error
      elsif response.not_found?
        error = NotFoundError.new response.message, response, **not_found_id
        not_found.call error: error, response: response, **not_found_id
        raise error
      elsif response.invalid_opengraph?
        raise InvalidOpenGraphError.new response.message, response
      elsif response.transient? || response.unknown?
        raise RetriableError.new response.message, response
      else
        raise FacebookError.new response.message, response
      end
    end
    def migrated_ids
      @_migrated_ids ||= parse_migrated_ids
    end
    def parse_migrated_ids
      matches = response.message.match(/Page ID (\d+) was migrated to page ID (\d+)\./)
      old_id, new_id = *matches.to_a.drop(1)
      { old_id: old_id, new_id: new_id }
    end
    def not_found_id
      @_not_found_id ||= parse_not_found_id
    end
    def parse_not_found_id
      matches = response.message.match(/Object with ID '([^']+)'/)
      id = matches[1] if matches
      { id: id }
    end
    def default_delegate
      lambda { |error:, response:, **options| }
    end
  end
end