class Octokit::Error
Custom error class for rescuing from all GitHub errors
Public Class Methods
error_for_401(headers)
click to toggle source
Returns most appropriate error for 401 HTTP status code @private
# File lib/octokit/error.rb, line 51 def self.error_for_401(headers) if Octokit::OneTimePasswordRequired.required_header(headers) Octokit::OneTimePasswordRequired else Octokit::Unauthorized end end
error_for_403(body)
click to toggle source
Returns most appropriate error for 403 HTTP status code @private
# File lib/octokit/error.rb, line 61 def self.error_for_403(body) if body =~ /rate limit exceeded/i Octokit::TooManyRequests elsif body =~ /login attempts exceeded/i Octokit::TooManyLoginAttempts elsif body =~ /returns blobs up to [0-9]+ MB/i Octokit::TooLargeContent elsif body =~ /abuse/i Octokit::AbuseDetected elsif body =~ /repository access blocked/i Octokit::RepositoryUnavailable elsif body =~ /email address must be verified/i Octokit::UnverifiedEmail elsif body =~ /account was suspended/i Octokit::AccountSuspended elsif body =~ /billing issue/i Octokit::BillingIssue elsif body =~ /Resource protected by organization SAML enforcement/i Octokit::SAMLProtected else Octokit::Forbidden end end
error_for_404(body)
click to toggle source
Return most appropriate error for 404 HTTP status code @private
# File lib/octokit/error.rb, line 87 def self.error_for_404(body) if body =~ /Branch not protected/i Octokit::BranchNotProtected else Octokit::NotFound end end
from_response(response)
click to toggle source
Returns the appropriate Octokit::Error
subclass based on status and response message
@param [Hash] response HTTP response @return [Octokit::Error]
# File lib/octokit/error.rb, line 10 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 415 then Octokit::UnsupportedMediaType when 422 then Octokit::UnprocessableEntity 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
new(response=nil)
click to toggle source
Calls superclass method
# File lib/octokit/error.rb, line 37 def initialize(response=nil) @response = response super(build_error_message) end
Public Instance Methods
documentation_url()
click to toggle source
Documentation URL returned by the API for some errors
@return [String]
# File lib/octokit/error.rb, line 45 def documentation_url data[:documentation_url] if data.is_a? Hash end
errors()
click to toggle source
Array of validation errors @return [Array<Hash>] Error
info
# File lib/octokit/error.rb, line 97 def errors if data && data.is_a?(Hash) data[:errors] || [] else [] end end
response_body()
click to toggle source
Body returned by the GitHub server.
@return [String]
# File lib/octokit/error.rb, line 122 def response_body @response[:body] end
response_headers()
click to toggle source
Headers returned by the GitHub server.
@return [Hash]
# File lib/octokit/error.rb, line 115 def response_headers @response[:response_headers] end
response_status()
click to toggle source
Status code returned by the GitHub server.
@return [Integer]
# File lib/octokit/error.rb, line 108 def response_status @response[:status] end
Private Instance Methods
build_error_message()
click to toggle source
# File lib/octokit/error.rb, line 172 def build_error_message return nil if @response.nil? message = "#{@response[:method].to_s.upcase} " message << redact_url(@response[:url].to_s) + ": " message << "#{@response[:status]} - " message << "#{response_message}" unless response_message.nil? message << "#{response_error}" unless response_error.nil? message << "#{response_error_summary}" unless response_error_summary.nil? message << " // See: #{documentation_url}" unless documentation_url.nil? message end
data()
click to toggle source
# File lib/octokit/error.rb, line 128 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 else nil end end
redact_url(url_string)
click to toggle source
# File lib/octokit/error.rb, line 185 def redact_url(url_string) %w[client_secret access_token].each do |token| url_string.gsub!(/#{token}=\S+/, "#{token}=(redacted)") if url_string.include? token end url_string end
response_error()
click to toggle source
# File lib/octokit/error.rb, line 153 def response_error "Error: #{data[:error]}" if data.is_a?(Hash) && data[:error] end
response_error_summary()
click to toggle source
# File lib/octokit/error.rb, line 157 def response_error_summary return nil unless data.is_a?(Hash) && !Array(data[:errors]).empty? summary = "\nError summary:\n" 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
response_message()
click to toggle source
# File lib/octokit/error.rb, line 144 def response_message case data when Hash data[:message] when String data end end