Exception: Octokit::Error
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
AbuseDetected, AccountSuspended, BadGateway, BadRequest, BillingIssue, BranchNotProtected, ClientError, CommitIsNotPartOfPullRequest, Conflict, Forbidden, InstallationSuspended, InternalServerError, MethodNotAllowed, NotAcceptable, NotFound, NotImplemented, OneTimePasswordRequired, PathDiffTooLarge, RepositoryUnavailable, SAMLProtected, ServerError, ServiceUnavailable, TooLargeContent, TooManyLoginAttempts, TooManyRequests, Unauthorized, UnavailableForLegalReasons, UnprocessableEntity, UnsupportedMediaType, UnverifiedEmail
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
StandardError
|
|
Instance Chain:
self,
StandardError
|
|
Inherits: |
StandardError
|
Defined in: | lib/octokit/error.rb |
Overview
Custom error class for rescuing from all GitHub errors
Class Method Summary
-
.error_for_401(headers)
Internal use only
Internal use only
Returns most appropriate error for 401 HTTP status code.
-
.error_for_403(body)
Internal use only
Internal use only
Returns most appropriate error for 403 HTTP status code.
-
.error_for_404(body)
Internal use only
Internal use only
Return most appropriate error for 404 HTTP status code.
-
.error_for_422(body)
Internal use only
Internal use only
Return most appropriate error for 422 HTTP status code.
-
.from_response(response) ⇒ Octokit::Error
Returns the appropriate
Error
subclass based on status and response message. - .new(response = nil) ⇒ Error constructor
Instance Attribute Summary
- #context readonly
Instance Method Summary
- #build_error_context
-
#documentation_url ⇒ String
Documentation URL returned by the API for some errors.
-
#errors ⇒ Array<Hash>
Array of validation errors.
-
#response_body ⇒ String
Body returned by the GitHub server.
-
#response_headers ⇒ Hash
Headers returned by the GitHub server.
-
#response_status ⇒ Integer
Status code returned by the GitHub server.
- #build_error_message private
- #data private
- #redact_url(url_string) private
- #response_error private
- #response_error_summary private
- #response_message private
Constructor Details
.new(response = nil) ⇒ Error
# File 'lib/octokit/error.rb', line 45
def initialize(response = nil) @response = response super( ) build_error_context end
Class Method Details
.error_for_401(headers)
Returns most appropriate error for 401 HTTP status code
# File 'lib/octokit/error.rb', line 60
def self.error_for_401(headers) if Octokit::OneTimePasswordRequired.required_header(headers) Octokit::OneTimePasswordRequired else Octokit::Unauthorized end end
.error_for_403(body)
Returns most appropriate error for 403 HTTP status code
# File 'lib/octokit/error.rb', line 70
def self.error_for_403(body) if body =~ /rate limit exceeded/i Octokit::TooManyRequests elsif body =~ /exceeded a secondary rate limit/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 elsif body =~ /suspended your access|This installation has been suspended/i Octokit::InstallationSuspended else Octokit::Forbidden end end
.error_for_404(body)
Return most appropriate error for 404 HTTP status code
# File 'lib/octokit/error.rb', line 100
def self.error_for_404(body) if body =~ /Branch not protected/i Octokit::BranchNotProtected else Octokit::NotFound end end
.error_for_422(body)
Return most appropriate error for 422 HTTP status code
# File 'lib/octokit/error.rb', line 110
def self.error_for_422(body) 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
.from_response(response) ⇒ Error
Returns the appropriate Error
subclass based
on status and response message
# File 'lib/octokit/error.rb', line 12
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 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
Instance Attribute Details
#context (readonly)
[ GitHub ]# File 'lib/octokit/error.rb', line 6
attr_reader :context
Instance Method Details
#build_error_context
[ GitHub ]# File 'lib/octokit/error.rb', line 39
def build_error_context if RATE_LIMITED_ERRORS.include?(self.class) @context = Octokit::RateLimit.from_response(@response) end end
#build_error_message (private)
[ GitHub ]# File 'lib/octokit/error.rb', line 195
def return nil if @response.nil? = +"#{@response[:method].to_s.upcase} " << redact_url(@response[:url].to_s.dup) + ': ' << "#{@response[:status]} - " << .to_s unless .nil? << response_error.to_s unless response_error.nil? << response_error_summary.to_s unless response_error_summary.nil? << " // See: #{documentation_url}" unless documentation_url.nil? end
#data (private)
[ GitHub ]# File 'lib/octokit/error.rb', line 153
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
#documentation_url ⇒ String
Documentation URL returned by the API for some errors
#errors ⇒ Array
<Hash
>
Array of validation errors
#redact_url(url_string) (private)
[ GitHub ]# File 'lib/octokit/error.rb', line 208
def redact_url(url_string) %w[client_secret access_token].each do |token| if url_string.include? token url_string.gsub!(/#{token}=\S+/, "#{token}=(redacted)") end end url_string end
#response_body ⇒ String
Body returned by the GitHub server.
# File 'lib/octokit/error.rb', line 147
def response_body @response[:body] end
#response_error (private)
[ GitHub ]#response_error_summary (private)
[ GitHub ]# File 'lib/octokit/error.rb', line 180
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_headers ⇒ Hash
Headers returned by the GitHub server.
# File 'lib/octokit/error.rb', line 140
def response_headers @response[:response_headers] end
#response_message (private)
[ GitHub ]
#response_status ⇒ Integer
Status code returned by the GitHub server.
# File 'lib/octokit/error.rb', line 133
def response_status @response[:status] end