Exception: Octokit::Error
- Inherits:
-
StandardError
- Object
- StandardError
- Octokit::Error
- Defined in:
- lib/octokit/error.rb
Overview
Custom error class for rescuing from all GitHub errors
Direct Known Subclasses
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Class Method Summary collapse
-
.error_for_401(headers) ⇒ Object
Returns most appropriate error for 401 HTTP status code rubocop:disable Naming/VariableNumber.
-
.error_for_403(body) ⇒ Object
Returns most appropriate error for 403 HTTP status code.
-
.error_for_404(body) ⇒ Object
Return most appropriate error for 404 HTTP status code rubocop:disable Naming/VariableNumber.
-
.error_for_422(body) ⇒ Object
Return most appropriate error for 422 HTTP status code rubocop:disable Naming/VariableNumber.
-
.from_response(response) ⇒ Octokit::Error
Returns the appropriate Octokit::Error subclass based on status and response message.
Instance Method Summary collapse
- #build_error_context ⇒ Object
-
#documentation_url ⇒ String
Documentation URL returned by the API for some errors.
-
#errors ⇒ Array<Hash>
Array of validation errors.
-
#initialize(response = nil) ⇒ Error
constructor
A new instance of Error.
-
#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.
Constructor Details
#initialize(response = nil) ⇒ Error
Returns a new instance of Error.
46 47 48 49 50 |
# File 'lib/octokit/error.rb', line 46 def initialize(response = nil) @response = response super() build_error_context end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
6 7 8 |
# File 'lib/octokit/error.rb', line 6 def context @context end |
Class Method Details
.error_for_401(headers) ⇒ Object
Returns most appropriate error for 401 HTTP status code rubocop:disable Naming/VariableNumber
62 63 64 65 66 67 68 69 |
# File 'lib/octokit/error.rb', line 62 def self.error_for_401(headers) # rubocop:enbale Naming/VariableNumber if Octokit::OneTimePasswordRequired.required_header(headers) Octokit::OneTimePasswordRequired else Octokit::Unauthorized end end |
.error_for_403(body) ⇒ Object
Returns most appropriate error for 403 HTTP status code
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/octokit/error.rb', line 73 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 |
.error_for_404(body) ⇒ Object
Return most appropriate error for 404 HTTP status code rubocop:disable Naming/VariableNumber
104 105 106 107 108 109 110 111 |
# File 'lib/octokit/error.rb', line 104 def self.error_for_404(body) # rubocop:enable Naming/VariableNumber if body =~ /Branch not protected/i Octokit::BranchNotProtected else Octokit::NotFound end end |
.error_for_422(body) ⇒ Object
Return most appropriate error for 422 HTTP status code rubocop:disable Naming/VariableNumber
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/octokit/error.rb', line 116 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 |
.from_response(response) ⇒ Octokit::Error
Returns the appropriate Octokit::Error subclass based on status and response message
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/octokit/error.rb', line 13 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 Method Details
#build_error_context ⇒ Object
40 41 42 43 44 |
# File 'lib/octokit/error.rb', line 40 def build_error_context if RATE_LIMITED_ERRORS.include?(self.class) @context = Octokit::RateLimit.from_response(@response) end end |
#documentation_url ⇒ String
Documentation URL returned by the API for some errors
55 56 57 |
# File 'lib/octokit/error.rb', line 55 def documentation_url data[:documentation_url] if data.is_a? Hash end |
#errors ⇒ Array<Hash>
Array of validation errors
129 130 131 132 133 134 135 |
# File 'lib/octokit/error.rb', line 129 def errors if data.is_a?(Hash) data[:errors] || [] else [] end end |
#response_body ⇒ String
Body returned by the GitHub server.
154 155 156 |
# File 'lib/octokit/error.rb', line 154 def response_body @response[:body] end |
#response_headers ⇒ Hash
Headers returned by the GitHub server.
147 148 149 |
# File 'lib/octokit/error.rb', line 147 def response_headers @response[:response_headers] end |
#response_status ⇒ Integer
Status code returned by the GitHub server.
140 141 142 |
# File 'lib/octokit/error.rb', line 140 def response_status @response[:status] end |