module NeonRAW::Errors

Methods and classes for handling errors.

Public Instance Methods

assign_data_errors(errors) click to toggle source

Checks data for any errors that wouldn't have otherwise raised an exception. @!method assign_data_errors(errors) @param errors [Array<String>] The errors.

# File lib/NeonRAW/errors.rb, line 85
def assign_data_errors(errors)
  return nil if errors.empty?
  error = if errors.first == :row # handles flair errors
            errors[1]
          else
            errors.first
          end
  case error
  when /improperly formatted row/i then BadFlairRowFormat
  when /no_subject/i               then NoSubject
  when /too_long/i                 then TooLong
  when /no_text/i                  then NoText
  when /subreddit_noexist/i        then InvalidSubreddit
  when /user_muted/i               then UserMuted
  when /no_sr_to_sr_message/i      then InvalidSubreddit
  when /user_blocked/i             then UserBlocked
  when /muted_from_subreddit/i     then MutedFromSubreddit
  when /subreddit_notallowed/i     then PermissionDenied
  when /no_selfs/i                 then NoSelfPosts
  when /no_links/i                 then NoLinkPosts
  when /url is required/i          then NoUrl
  when /already been submitted/i   then AlreadySubmitted
  when /no_invite_found/i          then NoInviteFound
  when /deleted_comment/i          then DeletedComment
  when /thread_locked/i            then PermissionDenied
  when /image_error/i              then ImageError
  when /subreddit_exists/i         then SubredditExists
  when /cant_create_sr/i           then CantCreateSubreddit
  when /invalid_option/i           then InvalidOption
  when /gold_required/i            then GoldRequired
  when /gold_only_sr_required/i    then GoldOnlySrRequired
  when /admin_required/i           then PermissionDenied
  when /bad_number/i               then BadNumber
  when /bad_sr_name/i              then BadSubredditName
  when /rows per call reached/i    then TooManyFlairRows
  when /unable to resolve user/i   then CouldntResolveUser
  when /sr_rule_exists/i           then RuleExists
  when /sr_rule_too_many/i         then TooManyRules
  when /too_old/i                  then Archived
  end
end
assign_errors(response, json) click to toggle source

Reads the HTTP status of the Typhoeus response and gives an exception to raise. @!method assign_errors(response, json) @param response [Typhoeus::Response] The response object. @param json [Boolean] Whether or not the response is JSON. @return [StandardError, nil] Returns either the exception or nil if there

is none.
# File lib/NeonRAW/errors.rb, line 15
def assign_errors(response, json)
  code = response.code
  body = response.body
  case code
  when 200
    if json
      errors = JSON.parse(body, symbolize_names: true)
      errors.extend(Hashie::Extensions::DeepFind)
      errors = errors.deep_find(:error) || errors.deep_find(:errors)
      errors = errors.first if errors.is_a?(Array)
      case errors
      when /access_denied/i             then OAuth2AccessDenied
      when /unsupported_response_type/i then InvalidResponseType
      when /unsupported_grant_type/i    then InvalidGrantType
      when /invalid_scope/i             then InvalidScope
      when /invalid_request/i           then InvalidRequest
      when /invalid_grant/i             then InvalidCredentials
      when /wrong_password/i            then InvalidCredentials
      when /bad_captcha/i               then InvalidCaptcha
      when /ratelimit/i                 then RateLimited
      when /quota_filled/i              then QuotaFilled
      when /bad_css_name/i              then InvalidClassName
      when /too_much_flair_css/i        then TooManyClassNames
      when /user_required/i             then AuthenticationRequired
      when /bad_flair_target/i          then BadFlairTarget
      end
    end
  when 302 then UnexpectedRedirect
  when 400 then BadRequest
  when 401 then InvalidOAuth2Credentials
  when 403
    if /user_required/i =~ body
      AuthenticationRequired
    else
      PermissionDenied
    end
  when 404 then NotFound
  when 409 then Conflict
  when 413 then RequestTooLarge
  when 429 then RateLimited
  when 500 then InternalServerError
  when 502 then BadGateway
  when 503 then ServiceUnavailable
  when 504 then TimedOut
  when 520 then CouldntReachServer
  end
end
parse_errors(data) click to toggle source

Parses Reddit data for errors. @!method parse_errors(data) @param data [Array, Hash] The data.

# File lib/NeonRAW/errors.rb, line 66
def parse_errors(data)
  # handles returns from toggleable methods
  return assign_data_errors([]) if data.empty?
  data.extend(Hashie::Extensions::DeepFind)

  errors = data.deep_find(:errors)
  return assign_data_errors([]) if errors.nil? || errors.empty?
  return assign_data_errors(errors.first) if errors.first.is_a?(Array)
  if errors.is_a?(Hash)
    messages = errors.map { |_key, error| error } # handles multireddits
    return assign_data_errors(messages)
  end
  return assign_data_errors(errors)
end
update_ratelimit_info(headers) click to toggle source

Manages the API ratelimit for requesting stuff from Reddit. @!method update_ratelimit_info(headers) @param headers [Hash] The Typhoeus response headers.

# File lib/NeonRAW/errors.rb, line 130
def update_ratelimit_info(headers)
  @requests_remaining = headers['X-Ratelimit-Remaining'].to_i
  @ratelimit_reset = headers['X-Ratelimit-Reset'].to_i
end