class Octokit::Error

Custom error class for rescuing from all GitHub errors

def self.error_for_401(headers)

Other tags:
    Private: -
def self.error_for_401(headers)
  # rubocop:enbale Naming/VariableNumber
  if Octokit::OneTimePasswordRequired.required_header(headers)
    Octokit::OneTimePasswordRequired
  else
    Octokit::Unauthorized
  end
end

def self.error_for_403(body)

Other tags:
    Private: -
def self.error_for_403(body)
  # rubocop:enable Naming/VariableNumber
  case body
  when /rate limit exceeded/i, /exceeded a secondary rate limit/i
    Octokit::TooManyRequests
  when /login attempts exceeded/i
    Octokit::TooManyLoginAttempts
  when /(returns|for) blobs (up to|between) [0-9-]+ MB/i
    Octokit::TooLargeContent
  when /abuse/i
    Octokit::AbuseDetected
  when /repository access blocked/i
    Octokit::RepositoryUnavailable
  when /email address must be verified/i
    Octokit::UnverifiedEmail
  when /account was suspended/i
    Octokit::AccountSuspended
  when /billing issue/i
    Octokit::BillingIssue
  when /Resource protected by organization SAML enforcement/i
    Octokit::SAMLProtected
  when /suspended your access|This installation has been suspended/i
    Octokit::InstallationSuspended
  else
    Octokit::Forbidden
  end
end

def self.error_for_404(body)

Other tags:
    Private: -
def self.error_for_404(body)
  # rubocop:enable Naming/VariableNumber
  if body =~ /Branch not protected/i
    Octokit::BranchNotProtected
  else
    Octokit::NotFound
  end
end

def self.error_for_422(body)

Other tags:
    Private: -
def self.error_for_422(body)
  # rubocop:enable Naming/VariableNumber
  if body =~ /PullRequestReviewComment/i && body =~ /(commit_id|end_commit_oid) is not part of the pull request/i
    Octokit::CommitIsNotPartOfPullRequest
  elsif body =~ /Path diff too large/i
    Octokit::PathDiffTooLarge
  else
    Octokit::UnprocessableEntity
  end
end

def self.from_response(response)

Returns:
  • (Octokit::Error) -

Parameters:
  • response (Hash) -- HTTP response
def self.from_response(response)
  status  = response[:status].to_i
  body    = response[:body].to_s
  headers = response[:response_headers]
  if klass =  case status
              when 400      then Octokit::BadRequest
              when 401      then error_for_401(headers)
              when 403      then error_for_403(body)
              when 404      then error_for_404(body)
              when 405      then Octokit::MethodNotAllowed
              when 406      then Octokit::NotAcceptable
              when 409      then Octokit::Conflict
              when 410      then Octokit::Deprecated
              when 415      then Octokit::UnsupportedMediaType
              when 422      then error_for_422(body)
              when 451      then Octokit::UnavailableForLegalReasons
              when 400..499 then Octokit::ClientError
              when 500      then Octokit::InternalServerError
              when 501      then Octokit::NotImplemented
              when 502      then Octokit::BadGateway
              when 503      then Octokit::ServiceUnavailable
              when 500..599 then Octokit::ServerError
              end
    klass.new(response)
  end
end

def build_error_context

def build_error_context
  if RATE_LIMITED_ERRORS.include?(self.class)
    @context = Octokit::RateLimit.from_response(@response)
  end
end

def build_error_message

def build_error_message
  return nil if @response.nil?
  message = +"#{@response[:method].to_s.upcase} "
  message << "#{redact_url(@response[:url].to_s.dup)}: "
  message << "#{@response[:status]} - "
  message << response_message.to_s unless response_message.nil?
  message << response_error.to_s unless response_error.nil?
  message << response_error_summary.to_s unless response_error_summary.nil?
  message << " // See: #{documentation_url}" unless documentation_url.nil?
  message
end

def data

def data
  @data ||=
    if (body = @response[:body]) && !body.empty?
      if body.is_a?(String) &&
         @response[:response_headers] &&
         @response[:response_headers][:content_type] =~ /json/
        Sawyer::Agent.serializer.decode(body)
      else
        body
      end
    end
end

def documentation_url

Returns:
  • (String) -
def documentation_url
  data[:documentation_url] if data.is_a? Hash
end

def errors

Returns:
  • (Array) - Error info
def errors
  if data.is_a?(Hash)
    data[:errors] || []
  else
    []
  end
end

def initialize(response = nil)

def initialize(response = nil)
  @response = response
  super(build_error_message)
  build_error_context
end

def redact_url(url_string)

def redact_url(url_string)
  %w[client_secret access_token api_key].each do |token|
    if url_string.include? token
      url_string.gsub!(/#{token}=\S+/, "#{token}=(redacted)")
    end
  end
  url_string
end

def response_body

Returns:
  • (String) -
def response_body
  @response[:body]
end

def response_error

def response_error
  "Error: #{data[:error]}" if data.is_a?(Hash) && data[:error]
end

def response_error_summary

def response_error_summary
  return nil unless data.is_a?(Hash) && !Array(data[:errors]).empty?
  summary = +"\nError summary:\n"
  return summary << data[:errors] if data[:errors].is_a?(String)
  summary << data[:errors].map do |error|
    if error.is_a? Hash
      error.map { |k, v| "  #{k}: #{v}" }
    else
      "  #{error}"
    end
  end.join("\n")
  summary
end

def response_headers

Returns:
  • (Hash) -
def response_headers
  @response[:response_headers]
end

def response_message

def response_message
  case data
  when Hash
    data[:message]
  when String
    data
  end
end

def response_status

Returns:
  • (Integer) -
def response_status
  @response[:status]
end